Getting a variable of a referenced Game Object, which is set in a global variable

I realize that the phrasing of the question is probably a bit confusing, but I couldnt think of better wording.

I know how to access an object’s variables through another object, but I’m running into a problem when trying to get the variable of another object, which is set as a global variable, and get that object’s variable.

What I’m trying to do is take firstClickedNode from the global variables script (see below) and pull a variable off of its instance. So pulling from the code below, I tried

globalVar.firstClickedNode.option1;

This gives an error saying: “Object reference not set to instance of object”

I’ve also tried using a GetOptions function and running the function instead of the variable directly but that gave similar issues.

Is there something you have to do differently when referencing an object that is set in a variable, in particular if it is global? Logically this should work to me…

So I have the GlobalVariables.js script:

static var nodeSelected = false;
static var firstClickedNode : GameObject;
static var secondClickedNode : GameObject;

And my script for the game:
The conditions/if statements work logically, its specifically accessing the option1 variable of firstClickedNode that is giving me issues.

var globalVar : GlobalVariables;
var globalGameObj : GameObject;

var isGoalNode : boolean;
var isBlue : boolean;
var isGreen : boolean;
var isRed : boolean;
var isTransfer1 : boolean;
var isTransfer2 : boolean;
var isTransfer3 : boolean;

var blueMat : Material;
var greenMat : Material;
var redMat : Material;
var trans1Mat : Material;
var trans2Mat : Material;
var trans3Mat : Material;

var wasClicked = false;

var option1 : GameObject;
var option2 : GameObject;
var option3 : GameObject;

var lrStartPosition : Vector3;
var lrEndPosition : Vector3;

function Awake(){

    //disable the halo by default
    gameObject.GetComponent("Halo").enabled = false;
    //Check node settings and set up materials and properties
    if(isGoalNode == true){
       if(isBlue == true){
         renderer.material = blueMat;
       }
       else if(isGreen == true){
         renderer.material = greenMat;
       }
       else if(isRed == true){
         renderer.material = redMat;
       }
    }
    else{
       if(isTransfer1 == true){
         renderer.material = trans1Mat;
       }
       else if (isTransfer2 == true){
         renderer.material = trans2Mat;
       }
       else if(isTransfer3 == true){
         renderer.material = trans3Mat;
       }
    }

}



//Highlight for when object is selected
function Highlight(){
    globalVar.nodeSelected = true;
    gameObject.GetComponent("Halo").enabled = true;
}

//Deselection
function Unhighlight(){
    gameObject.GetComponent("Halo").enabled = false;
    globalVar.nodeSelected = false;
    wasClicked = false;
}


//Set Global Variable
function SetTargetNode(){
    globalVar.secondClickedNode = this.gameObject;
    //print(globalVar.secondClickedNode);
}

function GetOptions(){
    return option1;
}




function OnMouseDown(){
    if(globalVar.nodeSelected == false){
       if(wasClicked == false){
         Highlight();
         globalVar.firstClickedNode = this.gameObject;
         wasClicked = true;
       }
    }
    else if(globalVar.nodeSelected == true){
       if(wasClicked == true){
         Unhighlight();
         globalVar.firstClickedNode = null;

       }
       else if(wasClicked == false){
         SetTargetNode();
         print("option 1 is: " + globalVar.firstClickedNode.option1);
         if(globalVar.secondClickedNode == globalVar.firstClickedNode.option1){   
          globalVar.firstClickedNode.BroadcastMessage("Unhighlight");
          print(globalVar.firstClickedNode);
          print(globalVar.secondClickedNode);
         }
         else{
          globalVar.secondClickedNode = null;
         }
       }
    }
}

You should be able to select the global variables script in the inspector.

I don’t have any issues with the global variables script itself. I can access firstClickedNode and secondClickedNode fine, and it prints the correct objects.

The problem comes when doing globalVar.firstClickedNode.option1 to get the variable of the object currently stored in the first clicked node.

The first clicked node changes everytime the player makes a decision, so it needs to validate the options associated with it. Right now it gives the error I said above when you try to access the option1 variable of the object stored in firstClickedNode.

http://www.youtube.com/watch?feature=player_embedded&v=-D6qXaGSC2k

link to debugging your unity code. enjoy.

I have gone through the code and know where the error occurs as stated above. Running through the debug isn’t helping me figure out why I can’t access the variable of the object stored in the global variable.

firstClickedNode properly assigns to the first node the user clicks. But doing should .option1 get the variable of that object?

I may be misunderstanding why you gave me the link to debugging, but I don’t see how that helps me figure out if I’m doing something wrong with syntax or if you have to do something special to access a variable, of a variable.

because the video will show you how to set a breakpoint, hit the breakpoint, and check variables of everything around your code. not sure if it works in JS though.

I’ve gone through that and the variable is set properly. firstClickedNode is set to “GreenNode” which was the node I clicked, and option1 on GreenNode is set to “BlueNode”

This isn’t helping me understand why I can’t call the:
function GetOption1(){
return option1;
}

function on firstClickedNode (the global variable). The object that is currently set to that variable has the function/method GetOption1, so why would it say it doesn’t have it?