Search Unity

Trouble accessing properties of remote class

Discussion in 'Scripting' started by reberk, May 26, 2017.

  1. reberk

    reberk

    Joined:
    Jul 27, 2012
    Posts:
    3
    I must be misunderstanding something about C# operation, as my attempts to reference the properties of a local instance of a remotely-defined class are resulting in NRE errors.

    The class looks like this:
    Code (CSharp):
    1.  
    2.     public class Dialogue{
    3.         public string iD;
    4.         public string text;
    5.     }
    6.  
    While my reference in another script is so:
    Code (csharp):
    1.  
    2.     dialogues.Add(default(OtherScript.Dialogue));
    3.     int cDial = curr.dialogues.Count-1;
    4.     curr.dialogues[cDial].text = "foo";
    5.  
    Assuming the rest of my script is working as intended (generous, I know, but I've gone through it with plenty of debug commenting and think the problem lies above), why might the attempt to assign to the text property of my newly-instantiated Dialogue throw a null reference exception?
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Maybe you want:
    Code (csharp):
    1.  
    2. Dialogue newdial = new Dialogue();   // or OtherScript.Dialogue if it's nested.
    3.  
    then the rest of the code (not sure what default is doing?) but other than that, it should be good.