Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by iHaveReturnd · Nov 14, 2012 at 12:57 AM · variablesinstanceglobal

Accessing a global variable's referenced object's 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:

 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;
             }
         }
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Vønce · Nov 14, 2012 at 01:13 AM

Just access this in your code:

GlobalVariables.firstClickedNode

The point of it being global is that you don't need to create an instance of it.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iHaveReturnd · Nov 14, 2012 at 01:20 AM 0
Share

That still leads to the same problem. Thanks for the advice for in general though. I still can't access the variable of the first clicked node.

To clarify, that first clicked node is something that changes as the player interacts with the objects in the scene, its not always the same object.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Calling a function across all instances of an object 2 Answers

Editing local variables in another script 2 Answers

Global Variables Refuse to Cooperate 1 Answer

function is faster when accessing function variables not global ones? 3 Answers

objects missing from array, on prefab, after updating list name in script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges