0 like 0 dislike
35 views
asked in Software by (5.7k points)

How I can to get certain variable of game object in Unity game engine?

unity gameobject get variable

Your answer

Try to answer the question as detailed as possible.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

1 Answer

0 like 0 dislike
answered by (5.7k points)

Just game object don't containt something variables. All possible variables contain in components, what a object have.

Accessing a variable on MonoBehaviour is normally a three step process:

  1. Get a reference to the appropriate GameObject.
  2. Get a reference to the appropriate Component with "GetComponent" method.
  3. Access a public member with a operator point.

Step one can often be skipped if you drag and drop references in the inspector, use a singleton, pass in a component reference, or any number of other approaches.

Example

GameObject.GetComponent<ClassName>().VariableName = 5;

Related questions

0 like 0 dislike
1 answer 46 views
0 like 0 dislike
1 answer 42 views
0 like 0 dislike
1 answer 1 view
0 like 0 dislike
1 answer 6 views
0 like 0 dislike
1 answer 11 views
0 like 0 dislike
1 answer 20 views
0 like 0 dislike
1 answer 35 views
0 like 0 dislike
1 answer 29 views
0 like 0 dislike
1 answer 30 views
...