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 4 days, 18 hours ago by Terence.
-
AuthorPosts
-
November 27, 2024 at 8:35 pm #16578::
Yes. The boss still making the pillar stack into 1 place only. I has tried some different way to fix it but still not work at all
November 27, 2024 at 9:43 pm #16581November 28, 2024 at 3:13 am #16586::here is the link of my project. Sorry for replying late
https://drive.google.com/file/d/1LWgEOmlEMzlzOTJFGvjGJZZn2IfaXOVH/view?usp=drive_linkNovember 28, 2024 at 1:45 pm #16595::I’d be glad to help you with the boss attack issue in your code. Here’s a breakdown of the problems and potential solutions, incorporating insights from the ratings:
Issues:-
Rapid Punches: The TriplePunch coroutine triggers the “Punch” animation three times in rapid succession using WaitForSeconds delays. This creates the illusion of very fast punches.
-
Null Reference Error: The PunchDamagePlayer function uses CorruptedMonk.Instance to access the monk’s properties, but it might be null if the CorruptedMonk object hasn’t been instantiated yet.
-
Animator Reset: The animation triggers are reset after each punch using anim.ResetTrigger(“Punch”), which might prevent the animation from playing smoothly if you intend for it to flow continuously during the triple punch sequence.
Solutions: -
Control Punch Speed: To slow down the punches, modify the delays within the TriplePunch coroutine. Instead of WaitForSeconds, consider using yield return new WaitUntil(() => // condition to wait for). Here’s an example:
IEnumerator TriplePunch() { attacking = true; rb.velocity = Vector2.zero;
// Wait until a certain animation frame is reached (adjust frame number as needed) yield return new WaitUntil(() => anim.GetCurrentAnimatorStateInfo(0).normalizedTime >= 0.5f);
anim.SetTrigger("Punch");
// Wait for a set duration (adjust as needed) yield return new WaitForSeconds(0.3f);
// Repeat for the second and third punches with appropriate delays // ...
ResetAllAttack(); }
-
Handle Null Reference: Ensure that the CorruptedMonk object exists before accessing it. You can add a check or use a safer reference approach:
-
Check for Existence:
void PunchDamagePlayer() { if (CorruptedMonk.Instance != null) { // Access CorruptedMonk properties here Hit(CorruptedMonk.Instance.sideAttackTransform, CorruptedMonk.Instance.sideAttackArea); } }
-
Safer Reference (if CorruptedMonk is a Singleton):
private CorruptedMonk corruptedMonk; void Start() { corruptedMonk = CorruptedMonk.Instance; }
-
void PunchDamagePlayer() { if (corruptedMonk != null) { Hit(corruptedMonk.sideAttackTransform, corruptedMonk.sideAttackArea); } }
- Consider Animation Flow: If you want the punches to blend smoothly, keep the animation triggers active (avoid ResetTrigger) and adjust delays to control the flow.
Additional Considerations: - Debugging: Use Debug.Log statements or the debugger to inspect values and identify where errors occur.
- Animation Speed: The animation speed can be controlled within the animation editor if the code doesn't directly set it.
- State Management: If Monk_State1 triggers other behaviors, ensure they don't interfere with the punch timing.
By combining these solutions and refining your code, you should be able to achieve the desired punch speed, avoid null reference errors, and potentially create a smoother animation flow.
has upvoted this post. November 28, 2024 at 2:16 pm #16596::I forgot about this. I had already handle the rapid punch. It because in the animation. I create 3 event for it with out knowing that i was make it 3. So i deleted it and the boss in no longer making ultra fast punch. The problem is only the null ref and the water pillar at 1 place. And the death sceen + resume in other topic. Glad if you could help me find out the way to fix the deathsceen and the resume issues. My project link in the above. You could check it out
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:
- Implement a pattern (e.g., circular, linear) to determine the spawn positions.
-
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.
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:
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/
-
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: