0 like 0 dislike
24 views
asked in Software by (4.7k points)

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

unity gameobject get variable

Your answer

pre, code

Preview

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 (4.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 23 views
0 like 0 dislike
1 answer 29 views
0 like 0 dislike
1 answer 20 views
0 like 0 dislike
1 answer 12 views
0 like 0 dislike
1 answer 26 views
0 like 0 dislike
1 answer 24 views
0 like 0 dislike
1 answer 25 views
0 like 0 dislike
1 answer 5 views
0 like 0 dislike
1 answer 8 views
...