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

Seemingly-Nonsensical Null Reference Error?

Discussion in 'Scripting' started by generalmcmutton, Jun 18, 2020.

  1. generalmcmutton

    generalmcmutton

    Joined:
    May 20, 2010
    Posts:
    42
    Hello there!

    I'm.... Super confused right now, because a section of code that worked perfectly fine a moment ago is suddenly broken, and the error it's throwing isn't conceivably possible.

    The error appears immediately after starting Play Mode

    The code in question is here:

    upload_2020-6-17_21-7-12.png

    And the error is here:

    upload_2020-6-17_21-7-36.png

    It's referencing line 421,
    animationComponent[currentAnimation].speed = 1;


    The initialization of the variables is:


    Code (CSharp):
    1. public string currentAnimation;
    2.  
    3. public Animation animationComponent;
    and the animationComponent is set directly in the inspector.


    I did some experimenting, and it seems the error is complaining about currentAnimation being null. But it never is- It starts out with text entered, and I've checked it with a bit of debug code, and it never triggered the text:


    Code (CSharp):
    1. if(currentAnimation == "")
    2.             print("What on Earth?");

    Any idea what could be going wrong?
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,004
    Have you checked that the string you have put in there is valid actually? I mean you can put "blksdbcfsd" in there and it is not empty, but it is unlikely that you have that in the animationComponent.
     
    generalmcmutton likes this.
  3. generalmcmutton

    generalmcmutton

    Joined:
    May 20, 2010
    Posts:
    42
    Thanks for the reply!

    Sadly it can't be that, since an improper animation name would throw a different error
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,004
    Well, throw in there a couple of Debug.Log, so you can find out what is actually null...
     
    generalmcmutton likes this.
  5. generalmcmutton

    generalmcmutton

    Joined:
    May 20, 2010
    Posts:
    42
    Oh, here we are! That's odd.

    Debug.Log/Print DID actually help- turns out that I was misinterpreting the nature of the error.

    I have a weapon-switching system, so the manner in which I play the proper animation for whatever weapon you have equipped is to get currentAnimation as a fragment of the animation name, and then add the weapon's name from a String together with it to get the correct animation name.

    I was thinking that any reference to an Animation component would throw the improper animation error, but it turns out that it'll throw a Null Reference error if I'm trying to alter an animation that doesn't exist via the Animation["Whatever"] syntax.

    We did it, Lurking-Ninja!

    Although it's still kind of odd- it was, indeed, working perfectly fine when I initially wrote the code...

    But either way.
     
    Lurking-Ninja likes this.
  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,004
    Yay!

    Well, I interpret this like an array somethingSomthing[foo].Bar -> if somethingSomthing[foo] returns with null instead of an animation object, it does not have .Bar member.
     
    generalmcmutton likes this.
  7. generalmcmutton

    generalmcmutton

    Joined:
    May 20, 2010
    Posts:
    42
    Ah-ha! Betcha that's exactly what the error came from.

    I recently switched from Unityscript, and find that I miss that language's errors since they gave you both a line number and where on that line it was.
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    The best way to effect this in C# is to just break up lines that have multiple bits to them, multiple places that COULD be null, and assign intermediate values to temp variables.

    At the end of the day, nobody charges you for extra vertical space in your code, plus the compiler is generally smart enough to generate the same output regardless of how many things you put on one line, in this case both an index dereference and a dot dereference.
     
    generalmcmutton likes this.
  9. generalmcmutton

    generalmcmutton

    Joined:
    May 20, 2010
    Posts:
    42
    Ah, that's an excellent idea!

    Even if not used as a general technique, it sounds really useful for debugging.