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

Viewing 7 posts - 21 through 27 (of 27 total)
  • Author
    Posts
  • #16597
    A_DONUT
    Level 7
    Moderator
    Helpful?
    Up
    0
    ::

    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);
        }

    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);
    • 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); 
    • 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.

    #16609
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    1
    ::

    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.
    #16610
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    I can’t get the boss to spawn the water pillars in your project though. Any suggestions?

    #16611
    pepecry
    Level 7
    Participant
    Helpful?
    Up
    0
    ::

    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

    #16628
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    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.

    #16630
    pepecry
    Level 7
    Participant
    Helpful?
    Up
    0
    ::

    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?

    #16631
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    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/

Viewing 7 posts - 21 through 27 (of 27 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: