Forum begins after the advertisement:
[Part 10] Boss hit very fast and Null ref
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [Part 10] Boss hit very fast and Null ref
- This topic has 26 replies, 3 voices, and was last updated 9 months ago by
Terence.
-
AuthorPosts
-
November 28, 2024 at 3:36 pm #16597::
I’d be glad to help you with the null reference and water pillar issues in your project. Null Reference Error: To address the null reference error, you’ll need to ensure that the CorruptedMonk.Instance is not null before accessing its properties. Here are a few ways to do this:
- Check for Null:
- Before accessing any properties of CorruptedMonk.Instance, check if it’s null:
if (CorruptedMonk.Instance != null) { // Access properties here Hit(CorruptedMonk.Instance.sideAttackTransform, CorruptedMonk.Instance.sideAttackArea); }
- Before accessing any properties of CorruptedMonk.Instance, check if it’s null:
Safer Reference (Singleton Pattern): If CorruptedMonk is a Singleton, you can store a reference to it in a variable:
private CorruptedMonk corruptedMonk; void Start() { corruptedMonk = CorruptedMonk.Instance; } void PunchDamagePlayer() { if (corruptedMonk != null) { Hit(corruptedMonk.sideAttackTransform, corruptedMonk.sideAttackArea); } }
Water Pillar at One Place: To make the water pillar appear at different locations, you’ll need to modify the code that determines its spawn position. Here are a few approaches:
-
Random Position:
- Generate a random position within a specified range:
Vector3 randomPosition = new Vector3(Random.Range(minX, maxX), Random.Range(minY, maxY), 0); Instantiate(waterPillarPrefab, randomPosition, Quaternion.identity);
- Generate a random position within a specified range:
-
Predefined Positions:
- Create an array of predefined positions and choose one randomly:
Vector3[] positions = { position1, position2, position3 }; int randomIndex = Random.Range(0, positions.Length); Instantiate(waterPillarPrefab, positions[randomIndex], Quaternion.identity);
- Create an array of predefined positions and choose one randomly:
-
Pattern-Based Placement:
- Implement a pattern (e.g., circular, linear) to determine the spawn positions. Additional Tips:
-
Debugging: Use Debug.Log statements to print variable values and track the execution flow.
-
Error Handling: Implement error handling mechanisms to catch potential exceptions and provide informative messages.
-
Testing: Thoroughly test your code to identify and fix issues early on.
-
Code Organization: Keep your code well-organized and readable.
-
Comments: Add comments to explain the purpose of different code sections. By following these suggestions and carefully reviewing your code, you should be able to resolve the null reference and water pillar issues. Please let me know if you have any other questions or require further assistance.
November 29, 2024 at 3:51 pm #16609::pepecry, for your resume issue, you need to assign the Game Manager in the Scene, not the Prefab in the Project window.
View post on imgur.com
has upvoted this post. November 29, 2024 at 4:00 pm #16610::I can’t get the boss to spawn the water pillars in your project though. Any suggestions?
November 29, 2024 at 4:13 pm #16611::You just need to delete or bring the spawn water pillar from phase 2 to phase 1 and replace it with sweepkich. I believe it will work. And tks you
November 29, 2024 at 11:46 pm #16628::Finally found what is causing your diving pillar issue, it is because your Animator for the water blast is controlling its position. Hence, even though your script is correct, the Animator always forces it to assume a fixed position beyond the first frame.
Remove the position keyframes on your water pillar animation and it will fix the issue:
View post on imgur.com
As for the Death Screen, make sure the button action uses the Game Manager object in the Scene, not the Prefab.
November 30, 2024 at 9:07 am #16630::Tks you terence but I want to ask another question. When I using the desolate dive, my character go through the ground. Is that because my character fall to fast and the groundcheck doesn’t make it on time? what should I do?
November 30, 2024 at 4:07 pm #16631::Yes, it’s possible your character is going too fast. You can set his Rigidbody’s Collision Detection Mode to Continuous Dynamic to fix this.
Read this to understand more about what this setting means: https://blog.terresquall.com/2019/12/collision-detection-modes-in-unitys-rigidbody-component/
- Check for Null:
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: