- Home /
Question by
Z2CSharp · Apr 17, 2013 at 09:28 PM ·
gameobjectvariableinspectorpublic
How to set a public variable to a gameObject in the inspector through coding
When I select a unit in my game I want it so that when I select a unit, the public variable Selected_Unit will be set to the gameObject that I have selected. I have already made a raycast that works but I just don't know how to set a public variable to a gameObject in the inspector by coding.
Comment
Does this work in C#, and for the selectedObject variable, is it public GameObject selectedObject;?
For a transform its public Transform selectedObject; selectedObject = hit.transform;
Best Answer
Answer by bionicnacho · Apr 17, 2013 at 09:43 PM
All you need to do is have a variable called selectedObject and assign it in the raycast with
selectedObject = hit.transform.gameobject;
Um - he want's a GameObject not a transform!
something.selectedObject = hit.transform.gameobject;