Quantcast
Channel: Answers by "Kerihobo"
Viewing all articles
Browse latest Browse all 78

Answer by Kerihobo

$
0
0
say you are trying to access a class called "MyClass", and that class has a variable: public int myInt; Well, if this class exists in your project folders somewhere, then that means engine can access it. soooo.... MyClass variableName = GameObject.Find("NameOfGameObjectInScene")(); note that GameObject.Find will run once, and if you have several objects with this name, you can't really be sure which GameObject with that name it will actually look at. Now, if that class is actually on the same object as the class you are trying to access it from: MyClass variableName = GetComponent(); I'm pretty sure that works, otherwise you may have to make it MyClass variableName = gameobject.GetComponent(); Now to actually make use of the variable, you'd go variableName.myInt = 5; or int someNewInt = variableName.myInt; Only PUBLIC variables can be accessed this way though. I believe if you are trying to access private variables of a componenent, you either need to trigger a public function in the receiving script, orrrr you need to make use of getters/setters

Viewing all articles
Browse latest Browse all 78

Trending Articles