- Home /
get variable from a different game object
Hi,
I'm trying to get the variable fixture from a script named object_Menu attached to a gameobject called fixture_01 could anyone explain to me the correct syntax for doing this? I'm struggling with this simple task.
cheers,
Answer by keefus · Nov 25, 2010 at 04:09 PM
Phew, solved the problem, heres the code if anyones interested!
private var fixtureobject : GameObject; private var fixtureselected = 0;
function Update ()
{ fixtureobject = GameObject.Find("fixture_01"); fixtureselected = fixtureobject.GetComponent(Object_Menu).fixture;
if (fixtureselected == 6) renderer.enabled = true;
if (fixtureselected != 6) renderer.enabled = false;
}
hope this helps somebody!
You might want to put the referencing (i.e. fixtureobject =, fixtureselected =, etc.) in the Start or Awake functions to avoid any unnecessary overhead when referencing the gameObject fixture_01 and script Object_$$anonymous$$enu every frame. Also, don't forget to mark your question answered, cheers!
This doesnt seem to work. I get the following error.
BCE0019: 'fixture' is not a member of 'UnityEngine.Component'.
Above code can also be shortened as: renderer.enabled = (fixtureselected == 6)
Your answer
![](https://koobas.hobune.stream/wayback/20220613084517im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Only change a variable on the instaniated object not the prefab. 0 Answers
Create a variable on a GameObject for access via the Object without GetComponent() 1 Answer
updating a gameobject variable from another script attached to another object 3 Answers
GetComponent() - Is it possible to pass a string variable as name of the script? 3 Answers
How to set a variable equal to another variable in another gameobject? 2 Answers