How to access position of an instantiated object's child object?

I have an instantiated gameobject named “Follower” that follows the player object. Inside Follower is an empty gameobject named “Exit.” I want to instantiate cubes at Exit’s position but the cubes only instantiate at the world origin point at 0, 1, 2 which is the position that Exit has when parented to Follower.

Whenever I move the player object, Follower, well, follows the player. When I press a button, cubes are supposed to instantiate a specific distance from Follower and the specific distance just so happens to be Exit’s position. However, it doesn’t happen.

Could someone please explain how I should access Exit’s position? I tried Exit.transform.position and localposition and still nothing.

There are multiple ways to accomplish this:

  1. In your Follower object, create a script with a public Transform Exit linked to the Exit child from the editor and grab the reference from that script.

  2. Loop through the children of the Follower on Awake and cache the results. You can achieve that with:

public Transform exit;

void Awake()
{
     foreach(Transform transform in follower.transform)
     {
          if(transform.tag == "exit")
          {
                exit = transform;
          }
    }
}

You don’t have to use the tag if you just assume your prefab only has one child object.

The weird thing is when I put a debug statement in the update function inside of your suggested script, I get an updating position. When I try to reference the exit variable inside my instantiating script, I still get the same coordinates.

Please post the script that you’ve got so far.

Inside Follower object:

public Transform ExitPoint;     // Store the exit point where objects will spawn

    // Use this for initialization
    void Start()
{

}

    // Update is called once per frame
    void FixedUpdate()
{
        Debug.Log("In Follower script, coordinates are: " + ExitPoint.transform.position);    // This is showing me the actual position of the object
}

Inside the script I want to spawn cubes at:

public void SpawnCubes()
{
        // Get the Object's exit point inside the GetExitPoint script
        Transform ExitPointPosition = ExitPoint.GetComponent<GetExitPoint>().ExitPoint.transform;

        // Get the rotation of the object's exit point      
        Quaternion ExitPointRotation = ExitPoint.GetComponent<GetExitPoint>().ExitPoint.transform.rotation;

        // This statement is showing me the position: 0, 1, 2 -- This is the original position of the Exit gameobject that is parented to the Follower object. 
        // Since the Follower object's position varies, and the Exit gameobject is in front of the Follower object, there should only be a slight difference in coordinates between the two debug statements
        Debug.Log("Inside this spawning script: " + ExitPointPosition.transform.position);
        ClonedCubes = (GameObject)Instantiate(CubeObject, ExitPointPosition.position, ExitPointRotation);
}

I’m a little confused here. Have you linked the ExitPoint via the editor? ExitPoint.position should work just fine but you’re pulling some GetExitPoint component from the ExitPoint just to reference the same ExitPoint’s transform? It’s very confusing.

I just wrote this quick test script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnCubes : MonoBehaviour
{
   // our prefab
   public GameObject CubeObject;

   // exit point
   public Transform ExitPoint;   

   protected void Start()
   {
     if (ExitPoint == null)
       Debug.Log("ExitPoint not linked!  Establish a connection in the editor.");
   }

   protected void Update()
   {
     // spawn a cube when we press space
     if (Input.GetKeyDown(KeyCode.Space) == true)
       SpawnCube();
   }

   public void SpawnCube()
   {
     Debug.Log("Inside this spawning script: " + ExitPoint.position);

     GameObject ClonedCubes = Instantiate(CubeObject, ExitPoint.position, ExitPoint.rotation) as GameObject;
   }
}

And here’s what my test object looks like in the inspector:


And here’s the results after spawning a cube cubes at the end of the “barrel” after some rotations:

Hope this helps.

I have it linked not in the hierarchy, but they are linked in the project folder. When I run the game, the Follower object is instantiated. I’ll try again.

Edit: Ok, I think I found the problem, I have the Follower prefab object linked to my spawn script object in the project folder and not in the hierarchy which is causing the problem I’m getting. However, I would prefer not to have to link an object via hierarchy and would prefer if my prefabs were linked in the project folder. Is there a way to get it working like that? If not, what are the other ways you mentioned?

Now I’m more confused, heh.

Is ExitPoint not simply an empty GameObject denoting a point in space you’d like to spawn cubes from, like say the barrel of a gun? You specifically mentioned that it was a child of another GameObject so I’m not sure why you’re against linking them in the hierarchy and saving them together as one prefab. That’s the preferred way of doing it.

If you’re instantiating ExitPoints dynamically or they’re attached to something else, then we’ll need to tackle the problem in another way.

I’m happy to help but please try to clarify exactly what’s going on. Maybe provide screenshots of your editor / inspector view to help us figure this out.

I found the problem. Basically, I had the prefab Follower object assigned to my spawn script’s gameobject variable in the project folder and not in the hierarchy. I pressed play and manually assigned the instantiated Follower object to the spawn script object and tested it out and it worked fine. I’m certain that by assigning the prefab Follower to the prefab spawn script object, the script was looking at the prefabbed coordinates and not the instantiated Follower object’s coordinates.

I think this is the logic behind my mistake:

  1. Follower prefab is assigned to the spawn script prefab inside the project folder section.
  2. Other script instantiates the Follower object (clone) at runtime. The Follower object (clone) can move around and have its coordinates update normally.
  3. Spawn script prefab looks at the Follower prefab object in the project folder section, it’s not looking at the Follower object (clone).

Now it’s a matter of figuring out how I’m going to reference location of the clone object that was instantiated into the scene instead of the location of the prefab object inside the folder.

If you need the reference to the exit object in the same script that instantiates the follower, just iterate over the clone’s children (in the second post, @GroZZleR has shown how to do that). So when you instantiate your prefab, get its transform and use the code provided above.

In case you need it somewhere else, you could use GameObject.Find / FindWithTag or make the exit object ‘register’ itself to a variable where you could pull the reference from. Question of design / use case.