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.

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;

1 Answer

1

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;

oh yeah! Whoops :P I'll update the code.