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.
1 Answer
1All 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;
– whydoidoit
Does this work in C#, and for the selectedObject variable, is it public GameObject selectedObject;?
– Z2CSharpFor a transform its public Transform selectedObject; selectedObject = hit.transform;
– Z2CSharp