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:
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;
}
}
}
}