Get & store value from another script

Hi, in my script Im trying to get/import a value from another file. My game is supposed to be a 3rd person, free view kind of game, where you can control some units, access inventory, see stats etc. Im new to C# i have programmed php for a long time, but find c# has a steep learning curve. i can’t quite grasp my head around all these types and functions. so i have turned to this community because i have found many great answers here.

This picture should explain what im wanting to do.

alt text

What im trying to achieve is:

  1. Select the unit, get the units name, and store it(until deselcted)
  2. Store the name so its accessible to all scripts i have that need to target the gameobject.
  3. retrieve the gameobject name, and handle it as a gameobject(i guess gameobject.find(name) would be suitable)

I have tried so many different things, but my novice experience with unity and c# holds me back.

How would i be able to achieve what the image displays?

It’s more convenient to just store the selected gameobject as a whole instead of just the name. GameObject.find(name) is pretty resource intensive, and multiple units might share the same name (I assume that each tank won’t have it’s own name like Jack, Peter, Susan and so on ;))

I can’t format code blocks at the moment s you just gotta use the script as it is presented here.

 GlobalStorage.cs:
public static GameObject selectedUnit;

UnitSelect.cs:
public LayerMask unitLayerMask;//select the layer of the unit

void Update() {

	Ray mouseRay = Camera.main.ScreenPointToRay (Input.mousePosition);
	RaycastHit hit;

	if(Physics.Raycast(mouseRay, out hit, Mathf.Infinity, unitLayerMask) == true) {
		GlobalStorage.selectedUnit = hit.collider.gameObject;
	}
}

GlobalStorage has to be a public static class that doesn’t extend MonoBehavior, and it also can’t be added to any objects (Unity won’t allow it anyway).