- Home /
Using .FindChild(x) where x is a variable
I need to find a way where you can access variables from a script on a Game Object, when the Game Object is not defined. I know I may not be explaining this well so here's an example of the code I tried that I know is wrong, but it kinda shows what I'm trying to do:
var x = "hello";
var y = "";
update () {
y = transform.findChild(x).getComponent(myScript).myVar
}
Answer by Graham-Dunnett · Jun 20, 2013 at 03:15 PM
y = transform.Find(x).GetComponent(myScript).myVar;
There are lots of ways that this code can fail - so you might want to check it bit by bit, meaning, can you find a child with the name x. Does that child have a script called myScript.
Yeah the child x would have a Script called myScript which has a variable called $$anonymous$$yVar
Your answer
Follow this Question
Related Questions
Passing a Script Name to a Function 2 Answers
Weapon script 0 Answers
Editing a variable from another script on collision 3 Answers
Declaring Variable in Child & Access as Parent 2 Answers
How to set a variable equal to another variable in another gameobject? 2 Answers