gameObject.name returns a wrong name

Hi

I have a script that generates objects and give them a name based on a number:

++count;
var cubeInstance = Instantiate (cube,position,Quaternion.identity) as GameObject;
cubeInstance.name = count+"(clone)";

then i have a script attached to the created object that gets its name:

If i run these Debug.Logs in both scripts

object generating script

Debug.Log(cubeInstance.name);

Script attached to generated object:

Debug.Log(name);

I get this results:

1776(Clone) UnityEngine.Debug:Log(Object) movecube:Awake() (at Assets\movecube.js:50) UnityEngine.Object:Internal_InstantiateSingle(Object, Vector3, Quaternion) UnityEngine.Object:Internal_InstantiateSingle(Object, Vector3, Quaternion) UnityEngine.Object:Instantiate(Object, Vector3, Quaternion) makecube:Update() (at Assets\makecube.js:22)

(Filename: Assets/movecube.js Line: 50)

1(clone) UnityEngine.Debug:Log(Object) makecube:Update() (at Assets\makecube.js:24)

(Filename: Assets/makecube.js Line: 24)

1776(Clone) UnityEngine.Debug:Log(Object) movecube:Awake() (at Assets\movecube.js:50) UnityEngine.Object:Internal_InstantiateSingle(Object, Vector3, Quaternion) UnityEngine.Object:Internal_InstantiateSingle(Object, Vector3, Quaternion) UnityEngine.Object:Instantiate(Object, Vector3, Quaternion) makecube:Update() (at Assets\makecube.js:22)

(Filename: Assets/movecube.js Line: 50)

2(clone) UnityEngine.Debug:Log(Object) makecube:Update() (at Assets\makecube.js:24)

(Filename: Assets/makecube.js Line: 24)

1776(Clone) UnityEngine.Debug:Log(Object) movecube:Awake() (at Assets\movecube.js:50) UnityEngine.Object:Internal_InstantiateSingle(Object, Vector3, Quaternion) UnityEngine.Object:Internal_InstantiateSingle(Object, Vector3, Quaternion) UnityEngine.Object:Instantiate(Object, Vector3, Quaternion) makecube:Update() (at Assets\makecube.js:22)

(Filename: Assets/movecube.js Line: 50)

3(clone) UnityEngine.Debug:Log(Object) makecube:Update() (at Assets\makecube.js:24)

(Filename: Assets/makecube.js Line: 24)

1776(Clone) UnityEngine.Debug:Log(Object) movecube:Awake() (at Assets\movecube.js:50) UnityEngine.Object:Internal_InstantiateSingle(Object, Vector3, Quaternion) UnityEngine.Object:Internal_InstantiateSingle(Object, Vector3, Quaternion) UnityEngine.Object:Instantiate(Object, Vector3, Quaternion) makecube:Update() (at Assets\makecube.js:22)

(Filename: Assets/movecube.js Line: 50)

4(clone) UnityEngine.Debug:Log(Object) makecube:Update() (at Assets\makecube.js:24)

(Filename: Assets/makecube.js Line: 24)

1776(Clone) UnityEngine.Debug:Log(Object) movecube:Awake() (at Assets\movecube.js:50) UnityEngine.Object:Internal_InstantiateSingle(Object, Vector3, Quaternion) UnityEngine.Object:Internal_InstantiateSingle(Object, Vector3, Quaternion) UnityEngine.Object:Instantiate(Object, Vector3, Quaternion) makecube:Update() (at Assets\makecube.js:22)

(Filename: Assets/movecube.js Line: 50)

5(clone) UnityEngine.Debug:Log(Object) makecube:Update() (at Assets\makecube.js:24)

(Filename: Assets/makecube.js Line: 24)

1776(Clone) UnityEngine.Debug:Log(Object) movecube:Awake() (at Assets\movecube.js:50) UnityEngine.Object:Internal_InstantiateSingle(Object, Vector3, Quaternion) UnityEngine.Object:Internal_InstantiateSingle(Object, Vector3, Quaternion) UnityEngine.Object:Instantiate(Object, Vector3, Quaternion) makecube:Update() (at Assets\makecube.js:22)

(Filename: Assets/movecube.js Line: 50)

6(clone) UnityEngine.Debug:Log(Object) makecube:Update() (at Assets\makecube.js:24)

(Filename: Assets/makecube.js Line: 24)

The problem is because you're checking the object's name in it's own Awake() function, which is actually called as a direct result of the "Instantiate" command. (You can even see that this is the case if you look at the last line of the stack trace of the first result that you posted).

This means that in your "object generation" script, the instance's "Awake" function is executed before next line of your object generation script runs. The next line is of course the line which sets the name - so "Awake" is happening before the object has been renamed.

To solve this, have your instance examine its own name in Start() instead of Awake().

Start() is executed only after everything in the current frame has been completed, so by the time Start() occurs on your instance, your "generation" script will have completed everthing within the function which generates the objects.