Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved GameObject returned from Instantiate is not referencing the spawned object.

Discussion in 'Scripting' started by Tajition, Jun 5, 2022.

  1. Tajition

    Tajition

    Joined:
    Jul 29, 2015
    Posts:
    4
    I instantiated a GameObject using a prefab like so:

    Code (CSharp):
    1.  
    2.         int randomIndex = Random.Range(0, spawns.Length);
    3.         // Instantiate a target
    4.         GameObject targetGameObject = Instantiate(target, spawns[randomIndex], Quaternion.identity);
    5.         targetGameObject.transform.parent = transform;
    6.         targetGameObject.GetComponent<TargetMover>().setVelocity(new Vector2(5f, 0f));
    7.         Debug.Log(targetGameObject.GetInstanceID());
    8.         nextSpawnTime = Time.time + Random.Range(minTime, maxTime);
    target is public GameObject I assigned using the editor and the spawns are just vector3 array
    upload_2022-6-5_22-55-45.png

    The spawned target object has a script to move it to the right like this:
    Code (CSharp):
    1.  
    2.     Rigidbody2D rb;
    3.     Vector2 velocity;
    4.  
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.         rb = GetComponent<Rigidbody2D>();
    9.         velocity = Vector2.zero;
    10.     }
    11.  
    12.     public void setVelocity(Vector2 newVelocity)
    13.     {
    14.         velocity = newVelocity;
    15.         Debug.Log("Setting new velocity: " + newVelocity );
    16.     }
    17.  
    18.     public Vector2 getVelocity()
    19.     {
    20.         return velocity;
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void FixedUpdate()
    25.     {
    26.         Debug.Log("Fixed Update: " + velocity + "  --- " + this.GetInstanceID());
    27.         rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
    28.     }
    The problem is that this line gets executed and I can even check to see if I correctly set the new velocity after.
    targetGameObject.GetComponent<TargetMover>().setVelocity(new Vector2(5f, 0f));

    But it is referencing some other object not the one created in the hierarchy.
    upload_2022-6-5_23-1-38.png
    upload_2022-6-5_23-1-47.png

    The Fixed Update instanceID is the correct instantiated one, but instance returned from Instantiate doesn't even exist in the hierarchy. Is this correct behavior?

    Edit: Weird part is that setting transform parent works as expected.
    targetGameObject.transform.parent = transform;
     

    Attached Files:

    Last edited: Jun 5, 2022
  2. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    595
    Are you not just mixing up instanceID of gameObject with instanceID of one of the components for that object? I didn't verify it but since GetInstanceID() is method for UnityEngine.Object (which is base class for GameObject and MonoBehavior) it wouldn't be surprising if each component attached to a game object and the game object itself had unique instanceID. In your spawn object script try printing
    gameObject.GetInstanceID()
    instead of
    this.GetInstanceID()
    and see if they still differ then.
     
  3. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    595
    As for your actual issue with SetVelocity, the most likey cause is execution order. Does your code have any other part that is modifying velocity variable? Yes you have, it even sets it to 0 same as what you observe in debug log message. Could it be executed between instantiating FixedUpdate? Maybe. It would take only more Debug Log line to verify if the thing that might be overwriting the velocity gets executed.
     
  4. Tajition

    Tajition

    Joined:
    Jul 29, 2015
    Posts:
    4
    Ok you are right the doing gameObject.GetInstanceID() results in the same id being printed.

    Changing the velocity by getting the component is still not working though :/
     
  5. Tajition

    Tajition

    Joined:
    Jul 29, 2015
    Posts:
    4
    Yep you were right, moving the initial speed of zero from start to declaration solved the issue! Thanks man.