"NullReferenceException" error

Hi,I got a problem with the “NullReferenceException” error form this code:

function A(){

    var temp = new GameObject();
    temp.name = "red";
    colorArray[0] = temp;
}

function B(){
    var name : String = colorArray[0].ToString();

    Debug.Log(name); // it shows "red", no problem.
                 // and I confirm the "red" GameObject is on the Hierarchy.
	
    var obj : GameObject = GameObject.Find(name);
	
    Debug.Log(obj); // **it shows "null", it really confuse me**

    Destroy(obj); // error message from this line
}

In game mode, I actually see the “red” instance on the Hierarchy but why the function “GameObject.Find” doesn’t work?

You convert colorArray[0] to a string so the debug prints it and name is a string, but obj is a game object Find look for a game object named name but none exists. Only a string with that name exists.

More likely to be something like this but that does not make much sense.

function B(){
    var name : GameObject = colorArray[0];//Considering colorArray is of type GameObject

    Debug.Log(name.name); // it shows "red", no problem.
                 // and I confirm the "red" GameObject is on the Hierarchy.

    var obj : GameObject = GameObject.Find(name);

    Debug.Log(obj); // **it shows "null", it really confuse me**

    Destroy(obj); // error message from this line
}