Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Object not Null, yet I get "Object reference not set to an instance of an object"

Discussion in 'Scripting' started by Erenquin, Feb 9, 2020.

  1. Erenquin

    Erenquin

    Joined:
    Apr 9, 2018
    Posts:
    164
    Hello,
    I'm puzzled with this one.

    Code (CSharp):
    1. al.SetActive(true);
    2. Alien Aliencomp = al.GetComponent<Alien>();
    3. Debug.Log("Alien comp: " + Aliencomp != null);
    4. Aliencomp.TakeDamage();
    Debug output:
    True
    NullReferenceException: Object reference not set to an instance of an object

    Note that this snippet is run the the TearDown of my unit tests.

    Thanks for the help
     
    Last edited: Feb 9, 2020
  2. Technokid2000

    Technokid2000

    Joined:
    Dec 18, 2016
    Posts:
    36
    Ok, so (Aliencomp != null) is true, so it obviously has found the component "Alien" (I presume it's another script). If this is the case, then perhaps the issue isn't the line "Aliencomp.TakeDamage();", but rather something within the function "TakeDamage()" is throwing an error. If you could send a screenshot of the error that unity throws, I could tell you what line the error was taking place on

    What I think is happenning is that within the function "TakeDamage()", are you attempting to access a null variable like a string or list, which is throwing the error. However, without a screenshot of the unity error, I cannot confirm this
     
    Last edited: Feb 9, 2020
    Erenquin likes this.
  3. Erenquin

    Erenquin

    Joined:
    Apr 9, 2018
    Posts:
    164
    Thanks very much.

    For the sake of completeness here is the Debug.Log. It does not show any line number and wasn't helpful at all:
    Code (CSharp):
    1. TearDown : Unhandled log message: '[Exception] NullReferenceException: Object reference not set to an instance of an object'. Use UnityEngine.TestTools.LogAssert.Expect
    So indeed it was something happening within TakeDamage() or more specifically within the Coroutine() called by TakeDamage().

    Narrowing down:
    Code (CSharp):
    1. Debug.Log("explosion not null: " + (ExplosionEffect != null));
    2. Debug.Log("explosion component not null: " + (GetComponent<ParticleSystem>() != null));
    3. Debug.Log("Collider not null: " + (GetComponent<BoxCollider>() != null));
    4. ExplosionEffect.Play();
    The Explosion effect component is not null, but ...
    The ExplosionEffect reference initialized in the start is null (or becomes null).

    Code (CSharp):
    1.  private ParticleSystem ExplosionEffect;
    2. ExplosionEffect = GetComponent<ParticleSystem>();
    [Edit: Ok, I think I understand]
    My assumption is: as I SetActive(false) the object right away, and never SetActive(true), the "Start" is never called, and so the ExplosionEffect variable never initialized.
     
    Last edited: Feb 9, 2020
  4. Technokid2000

    Technokid2000

    Joined:
    Dec 18, 2016
    Posts:
    36
    It depends on how the particle system is being added. If it is added to an object in the editor, there shouldn't be an error. If however you are using another script to add the particle system, then it's entirely possible that this script runs before the other one. This is called script run order, and normally isn't an issue for most people.

    Alternatively, you could try adding the particle system if it's not found:

    Code (CSharp):
    1. ExplosionEffect = GetComponent<ParticleSystem>();
    2. if (ExplosionEffect  == null) { ExplosionEffect = AddComponent<ParticleSystem>(); }
     
  5. Erenquin

    Erenquin

    Joined:
    Apr 9, 2018
    Posts:
    164
    You answered while I was editing :)
    Thanks for the help.

    The particle system is part of the Prefab and not added in the editor.
     
  6. Technokid2000

    Technokid2000

    Joined:
    Dec 18, 2016
    Posts:
    36
    Ah, gotcha! Makes sense. Yeah, sometimes unity doesn't play nice with prefabs. But if it's working now, that's good to hear :)
     
    Erenquin likes this.
  7. Erenquin

    Erenquin

    Joined:
    Apr 9, 2018
    Posts:
    164
    Well my assumption is partly (only) correct, but I'm getting close.
    If I do this:
    Code (CSharp):
    1. newal.SetActive(true);
    2. aliens.Add(newal);
    3. newal.SetActive(false);
    "Start" is not called, but if I remove the "newal.SetActive(false);" line, "Start" is called.

    I suppose this is some kind of compiler optimization.
    As it sees I do nothing with the object in-between it does not start the lifecycle.

    Well, time to clean up everything with this knowledge and see.
     
  8. Erenquin

    Erenquin

    Joined:
    Apr 9, 2018
    Posts:
    164
    Moved the initialization in Awake.
    This solves this issue.
    (I have a nother one, but this is anothet topic)
     
    Technokid2000 likes this.