- Home /
Accessing variables of objects in scene and not in assets
If I wanted to grab a variable from an item in the scene would I use
var variable;
var gameObject : Transform;
variable = gameObject.GetComponent(scriptName).variableName;
or would I use something else?
Answer by clunk47 · Sep 14, 2013 at 06:41 PM
I wouldn't use gameObject as a variable name, since it's already a component of the object your script is attached to. I'm guessing you just did that for example's sake, though. So yes, you're on the right track, GetComponent is definitely the way to go. Check out the Unity Script Reference for more information.
Im storing the object that needs to access the variable in the scene into my assets so it is spawned later on. The transform I set for it clears itself... So it doesnt get the variable.
yourGameObject : GameObject;
yourTransform : Transform;
But
GameObject != Transform
But what would I use to grab the variables over a network? But that object is in the scene therefore not a transform. am I right?
Your answer
Follow this Question
Related Questions
The position of the object and its changes! Help me can not understand! 0 Answers
How to assign Transform to prefab ? 2 Answers
Change Script so that the object's position can be modified in the Editor/through other scripts? 1 Answer
Is it possible to loop through the variables of a custom object? 1 Answer