Why does transform.position only return 0?

I’ve got a simple script attached to my camera that instantiates a game object relative to the camera transform. Why would the instantiated game object be reporting it’s position at 0,0,0 when the inspector (and viewport) says otherwise?

void Update () {
        if (Input.GetMouseButtonUp(0))
        {
            Instantiate(testGameObject, transform.position + transform.forward * 8, transform.rotation);

            Debug.Log(testGameObject.transform.position);
        }
    }

localPosition has the same problem.

Because you are logging the position of the prefab (or source go), not the one just created. Instantiate creates and returns a new object.

1 Like

So what is the usual way of getting a reference to a game object you just created?

This doesn’t work:
testGameObject = (GameObject)Instantiate(testGameObjectPrefab, transform.position + transform.forward * 8, transform.rotation);
ArgumentException: The thing you want to instantiate is null.

Wait, never mind, that works. Thanks!