Search Unity

Strange error when copying array value from one object to another

Discussion in 'Scripting' started by dunedinginga, Dec 22, 2014.

  1. dunedinginga

    dunedinginga

    Joined:
    Dec 22, 2014
    Posts:
    4
    I'm currently unable to solve this problem, and would really appreciate any help!

    This error: "NullReferenceException: Object reference not set to an instance of an object" keeps popping up when trying to copy a value from one object's variable to another.

    I have a game object called Scene1 with this script called sceneDescription attached:

    Code (JavaScript):
    1. var sceneDescription : String;
    2. var sceneDescArray : String[];
    3. var scenePlayed : boolean;
    4.  
    5. function Awake () {
    6.     sceneDescArray = sceneDescription.Split("\n"[0]);
    7. }
    8.  
    9. function Update () {
    10. }
    I have another game object called SceneControl with this script called sceneControl attached:
    Code (JavaScript):
    1. import UnityEngine;
    2. import UnityEngine.UI;
    3. import System.Collections;
    4.  
    5. var globalVariablesObject : GameObject;
    6. var localGlobalVariables : globalVariables;
    7. var dialogState: int;
    8. var sceneStart : GameObject;
    9. var sceneCurrent : GameObject;
    10. var sceneDescription : String;
    11. var sceneDescArray : String[];
    12. var scenePlayed : boolean;
    13. var sceneCanvasText : Text;
    14.  
    15. function Awake () {
    16.     localGlobalVariables = globalVariablesObject.GetComponent(globalVariables);
    17.  
    18.     //init
    19.     sceneCurrent = sceneStart;
    20.     globalVariablesObject.GetComponent(globalVariables).sceneCurrent = sceneCurrent;
    21.     sceneDescArray = sceneCurrent.GetComponent(sceneDescription).sceneDescArray;
    22.     dialogState = localGlobalVariables.dialogState;
    23. }
    24.  
    But when I run I keep getting this error: "NullReferenceException: Object reference not set to an instance of an object"

    Not sure what I am doing wrong. I simply want to copy the sceneDescArray content from the first object to the second...

    Help!
     
    Last edited: Dec 22, 2014
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    Looks like your second objects 'Awake' function is probably being called before the first one is so the variables aren't set up yet. You could move the second objects 'Awake' into 'Start' which is called afterwards or redo your code so it'll initialise if it hasn't already been.

    http://docs.unity3d.com/Manual/ExecutionOrder.html
     
    dunedinginga likes this.
  3. dunedinginga

    dunedinginga

    Joined:
    Dec 22, 2014
    Posts:
    4
    Solved!

    Thanks for your help. Had to move the init code in my global variables object to the awake function, and the code in the other scripts to the start function.

    What was throwing me off, was that had two scripts using the code in start function, one worked and other didn't! Think what it came down to was one script was quite long, the other very short. The long script was obviously just taking a teeny wee bit longer to execute, giving the global variables time to init, whereas the short was not. Eeek!

    Anyway, fixed now and thanks again.
     
    Last edited: Dec 23, 2014