Instantiated GameObject Not In Correct Location

I have an enemy prefab that moves left at a certain speed. I made a variant of this prefab which does the same thing but spawns a Power Up container upon its death. As a prefab this works great and the power up spawns where the enemy was destroyed.

When I add this to my spawner is where the issue comes in. My power up seems to spawn at the 0,0 grid location, regardless where where the enemy was destroyed at. Below is part of my enemy HP script. My explosion spawns perfectly so I assumed I would just follow that and I should have been good.

if(hp <= 0)
        {
            Pooler.Spawn(explosion, this.transform.position, this.transform.rotation);
            gameLevelManager.AddPoints(scoreValue);
            Pooler.Despawn(gameObject);
            Pooler.Spawn(PowerUp, this.transform.position, PowerUp.transform.rotation);  <-- spawns at 0,0 when in spawner and not where enemy destoryed
        }

In my heirarchy I have an empty object called Enemies, then under that I have my Enounter#, then under that I have my spawnpoint(s). Can this have something to do with the heirarchy I setup? Some sort of child thing?

Also, here is my spawn utility if it helps.

public Transform[] spawnPoints;
    public GameObject[] enemyPrefabs;
   
   
    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Enemy_Trigger"))
        {
               Spawn();
        }
       
    }
       
    private void Spawn()
    {
           
        for (int i = 0; i < spawnPoints.Length; i++)
        {
        Pooler.Spawn(enemyPrefabs[0], spawnPoints[i].position, transform.rotation);
        }
    }

Hey!

It is hard to say without knowing what despawn() or spawn() does.

In case you are setting a parent for your spawned object maybe you want to keep the world using the second parameter of this method position Unity - Scripting API: Transform.SetParent

You can also for safety save the position of your object to a Vector3 (which is a struct so it will not be affected by any further code) just before you despawn() your gameobject.\

Vector3 temPos = gameobject.transform.position;

This way you can be sure that you have the correct position and if something still doesn’t work it must be a problem inside the Spawn() method logic.

I hope it helps!

Also note for the future that this kind of thing should be posted in the scripting forum because it has nothing to do with 2D features or issues.

Sorry about that, Ace. I honestly never really noticed the scripting forum down there in the list to be honest…

You did help me see what was wrong although I solved it a bit differently. I’m thinking that since my game object is being returned to my pool before I spawn the powerup it doesn’t know where the object is anymore which is why it defaults to 0.0.
Since all my enemies that spawn power ups have explosion prefabs I just decided to stick this script on the explosion instead. That has a transform since it hasn’t been returned to my pool yet. Works perfectly.

public class PwrUp_Spawn : MonoBehaviour
{
   public GameObject powerUp;
  
   void Start()
   {
       Pooler.Spawn(powerUp, this.transform.position, powerUp.transform.rotation);
   }
}