How can I add a variable to a GameObject? GameObjects have a series of variables (name, transform, ...) that can be accessed and modified from any script. How could I add a variable such as for example "color" or "type" that could be accessed or modified from other scripts. Thanks.
As every object in c# GameObject class inheritance Object class. If you right click on GameObject and select go to definition you will see that below.

In order to reach one object's variables you need a reference(not considering static variables). So if you want to change variables of one GameObject from other you need a ref.
On Unity inspector you can assign values to public variables of a script. Below you can see Obj end of the arrow. Now assign cube(is a GameObject ) to Obj.
After drag and drop

Now you can reach every subComponent of cube object. In red rectangle I reached it's Transform component and changed it's position.

I hope I understood your question correct.GL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With