How to access variable from other game object

I have seen a few threads about this topic, but all the solutions seem to be outdated. I’m relatively new to c#, so I apologize if this is something I should be able to easily find in the manual.

I’m simply trying to create a variable, (int) and allow other game objects to access it, read it, write it, etc…

I have worked primarily in Lite-C with 3D GameStudio before this, and I can say it’s just about the easiest thing to do in that language. However, I acknowledge the stronger tool-set in Unity, and I would like to give it a try. But here I am, stumped by something that seems so easy.

I have also read about how keeping parts of a script separate from each other is more stable, and I can see why that would be the case. However, it seems hard to write any kind of program without some access to Global variables.

The GetComponent command seems to be the best thing to look into, but it seems limited to accessing things about the game object itself, not variables called in the script.

The other thing is static variables, but again, the old solutions don’t seem to work anymore, and you have to use GetComponent for anything to happen… has anyone had this problem? Using Unity 5.0.1f MonoDevelop 4.0.1

1 Like

… I’m not sure how they would be, this kind of thing really hasn’t changed for years.

There are “tightly” linked approaches where you use references such that the two scripts directly know of one/each other, either configured in the inspector by hand of through script (GetComponent, Find, etc.), or you use “loosely” linked approaches such as events (unityevent, actions, “normal” c# events etc.).

The first is found in a lot of tutorials since it’s straight forward and quite newbie friendly, events being a more “intermediate” level (not entirely sure I believe that, but I’m on the other side of that particular learning bump these days. Canvas UI being event driven really helps with simple examples I think).

Throw in a little manager gameobject vs singleton debate for the structure of how to handle where the data is kept and you’ve got the same old same old that’s been asked over and over on the forums.

GetComponent is used to get references to another script, once you have the reference you can access anything with the appropriate access level (public etc.). It’s not limited to the current gameobject, you can call GetComponent on any gameobject or component to hop to another component. A lot of the times it’s just a case of working your way along the “stepping stones”.

1 Like

Get the Component and access it. Variable has to be Public though.

1 Like

GAMEOBJECT.GetComponent().VariableName = 4;

13 Likes

Accessing a variable on another MonoBehaviour is normally a three step process.

  • Get a reference to the appropriate GameObject
  • Get a reference to the appropriate Component with GetComponent
  • Access a public member with a .

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.

Here are some more high level musings on the topic.

2 Likes