Forum begins after the advertisement:


[Part14]NullReferenceException: Object reference not set to an instance of an ob

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part14]NullReferenceException: Object reference not set to an instance of an ob

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #12564
    Burhan
    Participant

    I have followed the instructions from the tutorial but it seems there is an error like this.

    The damage text never shown in my code.

    NullReferenceException: Object reference not set to an instance of an object
    GameManager+<GenerateFloatingTextCoroutine>d__31.MoveNext () (at Assets/Scripts/GameManager.cs:131)
    UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <cd14705e8f094afcb566740ab5e8c02d>:0)
    UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
    GameManager:GenerateFloatingText(String, Transform, Single, Single) (at Assets/Scripts/GameManager.cs:164)
    EnemyStats:TakeDamage(Single, Vector2, Single, Single) (at Assets/Scripts/Enemy/EnemyStats.cs:60)
    ProjectileWeaponBehaviour:OnTriggerEnter2D(Collider2D) (at Assets/Scripts/Weapons/Weapon Base/ProjectileWeaponBehaviour.cs:99)
    #12565
    Terence
    Keymaster

    Hi Burhan, make sure that you have assigned all the new properties on the GameManager.

    Properites in GameManager

    Failing that, check out this video to troubleshoot your NullReferenceException.

    #12567
    Burhan
    Participant

    I assigned all properties. But I still have same error.

    #12569
    Former Patron

    I have this too

    #12571
    Terence
    Keymaster

    Burhan, can you double click on the NullReferenceException in your Console? This should bring you to the line of code that is the issue on Visual Studio. Screenshot that and show me what line it is.

    #12572
    #12576
    Burhan
    Participant

    Code

    I get error in line 131.

    #12588
    Terence
    Keymaster

    Burhan,

    Add the following lines above line 131, run the game, and show me the result on the Console.

    Debug.Log("textObj: " + textObj);
    Debug.Log("instance: " + instance);
    Debug.Log("damageTextCanvas: " + damageTextCanvas);

    One of your variables on Line 131 is null, and that is what is causing the null reference.

    #12622
    Burhan
    Participant

    console log

    #12631
    Terence
    Keymaster

    Burhan, I found the issue. You have an extra portion in your code:

    textObj.transform.parent.SetParent(instance.damageTextCanvas.transform);

    textObj has no parent, so when you try to access its parent, you are essentially doing null.SetParent(), which is what causes the code to fail.

    #12692
    Burhan
    Participant

    Thank you, problem solved.

    #12724
    Terence
    Keymaster

    No problem Burhan.

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: