- Home /
How to assign an object to variable C#?
Im kinda new to c#. I used to work with World Editor(warcraft 3)and there you could assign an object to a variable as simple as below
set udg_MyObject = gg_unit_hfoo_0000
Where "udg_MyObject" is a variable and "gg_unit_hfoo_0000" is an actual object
I know that C# and War3 use kind of different script mechanics, so this is what i want:
I want to have a variable that i could use in many different scripts that will refer to the object i need.(the target object will also change in game so i need to use variable for it)
Answer by tBureck · Feb 26, 2014 at 12:32 PM
"Kind of different" hits the nail on the head :) In C# every variable has a type, which you need to know beforehands. Assuming you have a GameObject type (which you usually have in Unity), you could create a variable like that:
GameObject myGameObject = referenceToAGameObject;
where myGameObject would be your variable (of type GameObject) and referenceToAGameObject would be a reference to your game object, which you can get from a Transform or whatever.
I think it would go too far if I explained that in detail here, so I'd just advise you to have a look at the Learn section on the Unity website, which provides a lot of videos explaining the very basics of Unity scripting with C#.
Thank you! exactly what i needed. Yeah, i know that going through the manual is best way to go, but im one of those people that learn by changing some random value and testing what it will do to the game :)))
Please use the very small "add new comment" button to add comments. Answers are meant to be solutions to the asked question.