How can I refer to a variable within a component, and not the variable's value?

Basically I want to make a script that changes a variable’s value. Except this value isn’t int, float, ect. It’s a GameObject.

So when I reference it it will reference the GameObject. But that’s not what I want, I want to change which game object is within the variable.

You can’t store a reference to a variable in another variable, but you can pass a variable by-reference so you can change the variables content within a function.

//C#

GameObject myGO = null;

void ModifyGO(ref GameObject aObject)
{
    aObject = new GameObject();
}

//[...]

Debug.Log(myGO != null);  // false
ModifyGO(ref myGO);
Debug.Log(myGO != null);  // true

This works with any kind of variable:

float myFloat = 0.0f;

void ModifyValue(ref float aValue)
{
    aValue = 5.0f;
}

//[...]

Debug.Log(myFloat);  // 0.0
Modifyvalue(ref myFloat);
Debug.Log(myFloat);  // 5.0

I’m not sure about the UnityScript syntax for ref parameters.

Based on my experience, all Unity’s internal Class Type need to allocate memory before using it. That is, a variable of Unity’s internal Class Type is actually a pointer like in C/C++. But a variable of user defined type could be a local variable that allocated memory on Stack rather than Heap.

Wait, so you have a gameObject variable, such as: public GameObject A;. You’ve maybe dragged in Robot1 to it in the Inspector, so now variable A “points to” Robot1. And you have been maybe using it to move Robot1 using A.transform.Translate(...);, things like that.

Now you would like to switch A to controlling Robot2? You use =. Seriously. You say A=(put new GameObject here). Now, A.transform.Translate(...); will affect the new gameObject.

The trick is, if A were a regular variable, that equals would copy Robot2 into Robot1, which would be bad. But GameObject vars aren’t like ints. They are really little arrows pointing to actual things. That’s what bunny was saying. Using = just changes what they “point to.”

The trick is, how do you find this new object. A typical use would be:

if(A==null) // my robot just died!!!
  A = GameObject.FindWithTag("robot"); // only works if I marked all robots

You can find lots of examples here about finding, say, nearest robot. Or finding whichever robot the user just clicked on, etc… .