Question by
taylom22 · Feb 03, 2016 at 11:18 PM ·
scripting problemscripting beginnervariablesglobal variable
I want to access a variable in another object's script whose GameObject is stored in a variable in script 1.
So, I have this in script 1 attached to my player object.
if(action){
actionVar = Physics2D.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer("Solid")).collider.gameObject;
Debug.Log ("actionVar is " + actionVar);
action = false;
}
On the npc object, I have script 2 attached. public static bool actionRec;
void Update () {
if (actionRec){
textBox.SetActive (true);
Debug.Log("action received");
}
}
Using the GameObject stored in actionVar (because it could change based on what object the linecast hits), how can I set the actionRec variable in script 2 to true from script 1?
Comment
Answer by cjdev · Feb 04, 2016 at 04:45 AM
Because it's static you can access it directly from the Component after you retrieve a reference to it:
GameObject.Find("npcObjectName").GetComponent<Script2>().actionRec = true;
// Where npcObjectName is the name of the GameObject your 2nd Script is on
// and Script2 is it's name
Your answer
Follow this Question
Related Questions
C# Unity dot syntax exercise correct solution? 1 Answer
The lifespan of an object instance in a Monobehavior script 0 Answers
Inherance script problem [C#] 0 Answers
My Character dosen´t move 0 Answers
Showing a high score in the menu 1 Answer