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. Dismiss Notice

Can get an object's property and log it in console BUT still Object reference not set to an instan

Discussion in 'Scripting' started by Ciigu, Mar 25, 2021.

  1. Ciigu

    Ciigu

    Joined:
    Mar 22, 2021
    Posts:
    3
    Guess it's a C# language problem.

    The code is like:
    Code (CSharp):
    1.  
    2. public interface ISkill
    3. {
    4.     public int PropA { get ; set ; }
    5. }
    6. public class  Skill_A : ISkill
    7. {
    8.     public int PropA { get ; set ; }
    9. }
    10. public class  Skill_B : ISkill
    11. {
    12.     public int PropA { get ; set ; }
    13. }
    14.  
    15. //(…)
    16. Public ISkill[] skillList = new ISkill[2];
    17. skillList[0]=new Skill_A() { PropA=1};
    18. skillList[1]=new Skill_B() { PropA=2};
    19. //(…)
    20. Debug.Log(skillList[0].GetType().ToString+','+skillList[0].PropA);
    21. Debug.Log(skillList[1].GetType().ToString+','+skillList[0].PropA);
    22.  
    The exception threw at "Debug.Log(skillList[0].GetType().ToString+','+skillList[0].PropA)"; ( also next line)
    The Console logged:
    Skill_A,1
    Skill_B,2
    which seems correct.
    But in the sametime there are NullReferenceException: Object reference not set to an instance of an object with them.
    I pretty sure the problem is that I used ISkill[] to create a collection of class Skill_A and class Skill_B.
    Just wanna kown is there a way I can use a collection to store and call different kinds of skills?
     
    Last edited: Mar 25, 2021
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Which line of code is throwing the exception? The error message contains a filename and a line number. Please include those.
     
  3. Ciigu

    Ciigu

    Joined:
    Mar 22, 2021
    Posts:
    3
    that's my fault.\
    The error is at "Debug.Log(skillList[0].GetType().ToString+','+skillList[0].PropA);".
    it did logged the correct information, but still throw that exception.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Not possible. This code must be running multiple times, potentially from multiple different instances.

    In one case, skillList has not been initialized. In the other case, it has been.
     
  5. Ciigu

    Ciigu

    Joined:
    Mar 22, 2021
    Posts:
    3
    Tried debug in VS and had comfirmed that skillList has 2 members of type ISkill and their properties were assigned
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Air your code out a little bit. Give it some room to breathe.

    Slip lots of Debug.Log() statements in for each part.

    Also, watch your consistency: For instance, the first debug is accessing [0] and [0] but the second one is [1] and [0], which seems odd.

    One thing per line, find out exactly what is throwing the null reference.

    Finally, my standard nullref blurb:

    The answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Also, did you retype this code?

    For one,
    public
    isn't valid inside an interface.

    For two, you spelled
    Public
    for your array declaration.

    Either you retyped this and hid your original problem from us, or else this code is not the code you think that is running.
     
    Joe-Censored likes this.