Issue with local and world positioning

I have looked at a lot of other posts about local vs global positioning and how everyone is saying that transform.position is supposed to return the global position. Somehow I’m having an issue with that and I’m not sure how.


Here is part of the code below. This part works just fine. I have some empty game objects that I’m using as placeholders to spawn different rooms randomly. Those templates are positioned in world space correctly and when I spawn a random room, it is in the position I expect it to be.


The issue I’m having is with a child object of the room itself. I have a script called “Room Properties” and that script has a few variables, the one in question is the “suspect spawn” which is just a GameObject.


I’m adding that gameObject to a List so that I can do something with it later. However, it seems the position of it is referenced in global space, not in reference to its parent prefab.


For example, if the spawn was (-2, 0, 0) from the parent of the prefab, so just a bit off center from the prefab, when I’m trying to instantiate objects at that location, it is instead spawning them in the global context and they are spawning roughly on top of each other.

foreach(GameObject template in mediumRoomsTemplates)
        {
            int rand = Random.Range(0, potentialOptions.mediumRoomPrefabs.Length);
            GameObject choice = potentialOptions.mediumRoomPrefabs[rand];

            Instantiate(choice, template.transform);
            //GameObject.Destroy(template);

            RoomProperties prop = choice.GetComponent<RoomProperties>();
            characterSpawns.Add(prop.suspectSpawn.gameObject);
            
        }

The part I don’t understand is this code. If I use the characterSpawnPoints array that I got from gameObjectWithTag, they will spawn in the correct position, however, if I use the list, characterSpawns which was the list that I was pushing those prefab child object spawn points to, the position is always based on the global (0,0,0) so the characters overlap each other. Those should be the same gameObjects, so I don’t understand why the position is different.

  GameObject[] characterSpawnPoints = GameObject.FindGameObjectsWithTag("Character Spawn Point");
  
        int counter = 0;
        foreach (GameObject suspect in sus)
        {
            GameObject spot = characterSpawns[counter];

            Instantiate(suspect, spot.transform.position, spot.transform.rotation);
            counter++;
        }

Can someone explain to me why I’m getting different positions? I assume it’s somehow related to how I’m adding those child spawns to the list, but I’m really not sure what I’m doing wrong.

Shortly after posting, I realized what the solution is. Since I build my prefabs at (0,0,0), this is why I am seeing it behave as it is. When I instantiate the room, I need to actually return a clone of that object that is created. This way I am actually getting the script off the object that is now positioned in the world space and therefore the referenced object is no longer referencing the prefab’s original positioning. Posting the solution in case anyone else has the issue.

            GameObject clone = Instantiate(choice, template.transform);
            //GameObject.Destroy(template);

            RoomProperties prop = clone.GetComponent<RoomProperties>();
            characterSpawns.Add(prop.suspectSpawn.gameObject);