Instantiate game object doesnt start at the origin

I have a game object that doesnt seem to want to instantiate anywhere I want it too, I’ve tried several things including passing in the location manually.

 Instantiate(enemy[roundNum], new Vector3(pos.x,pos.y,pos.z), Quaternion.identity);

The script is tied to a “start” object, which then spawns a enemy that has a controller attached to which moves towards the “end” object. Both object are within a prefab cube “playing field”.


It almost seems to me that the object are being instantiated under the playerArea position (0,0) , even though the start and end spheres are on opposite ends.

On top of that, they also always spawn on top of each other, which is strange to me. I’m slightly confused as to why.

public class EnemySpawn : MonoBehaviour
{
    public GameObject[] enemy;
    public Vector3 pos;
    public Transform end;
    public int roundNum = 0;
    public float spawnRate = 5.0f;
    public int totalEnemySpawn = 10;
    private bool waitingToStart = true;
    private int counter = 0;

    void Awake()
    {
        InvokeRepeating("Spawn", 0f, spawnRate);
    }
    void Spawn()
    {
        if (counter < totalEnemySpawn)
        {
            Instantiate(enemy[roundNum], new Vector3(pos.x,pos.y,pos.z), Quaternion.identity);
            counter++;

            waitingToStart = true;
        }
   }
   ... 
}

When I enter the vector position, it doesnt affect the spawning of the object in any way. Any suggestions?

If you instantiate a prefab without giving a position it will use the position that is on the prefab. If you are passing in a position it will spawn at that position, which means that something else is moving your objects as soon as they are made. Go through all of the scripts attached to the prefab and see if anything is moving the position of the object on Awake, Start, or Update. It could also be a script that has a reference to that prefab. Its hard to track down because everything has access to the transform of the object.

The thing is the Sphere is a child of the parent, meaning it will inherit the parents position as well. Meaning the code:

new Vector3(pos.x,pos.y,pos.z)

Is actually:

new Vector3(Parent.pos.x + pos.x,Parent.pos.y + pos.y,Parent.pos.z  + pos.z)

So try to Instantiate it at Vector3.Zero

I found that I have to do a one time SetPositionAndRotation in the update to get this to work.