GameObject returned from Instantiate is not referencing the spawned object.

I instantiated a GameObject using a prefab like so:

        int randomIndex = Random.Range(0, spawns.Length);
        // Instantiate a target
        GameObject targetGameObject = Instantiate(target, spawns[randomIndex], Quaternion.identity);
        targetGameObject.transform.parent = transform;
        targetGameObject.GetComponent<TargetMover>().setVelocity(new Vector2(5f, 0f));
        Debug.Log(targetGameObject.GetInstanceID());
        nextSpawnTime = Time.time + Random.Range(minTime, maxTime);

target is public GameObject I assigned using the editor and the spawns are just vector3 array
8182268--1065812--upload_2022-6-5_22-55-45.png

The spawned target object has a script to move it to the right like this:

    Rigidbody2D rb;
    Vector2 velocity;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        velocity = Vector2.zero;
    }

    public void setVelocity(Vector2 newVelocity)
    {
        velocity = newVelocity;
        Debug.Log("Setting new velocity: " + newVelocity );
    }

    public Vector2 getVelocity()
    {
        return velocity;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        Debug.Log("Fixed Update: " + velocity + "  --- " + this.GetInstanceID());
        rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
    }

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().setVelocity(new Vector2(5f, 0f));

But it is referencing some other object not the one created in the hierarchy.
8182268--1065815--upload_2022-6-5_23-1-38.png
8182268--1065818--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;

8182268--1065818--upload_2022-6-5_23-1-47.png

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.

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.

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 :confused:

Yep you were right, moving the initial speed of zero from start to declaration solved the issue! Thanks man.