Instantiated object not keeping transform of the instantiator after instantiator is destroyed

Hi all, first time using the forum and I’m a proper noob that has been smacking my head against a wall for the last 2 days with this one so any help is appreciated.

The Set Up:

  • A GameObject enemy prefab which instantiates the eggs using a coroutine and random time (I don’t think this is causal in any way).

  • Egg prefab with Spitball script (shown below)

Uses Transform; Rigidbody; Sphere Collider

In the code the public GameObject spider is the baby spider prefab to be instantiated and the public Transform spawnLoc takes the egg prefab to get the Transform (I think).

  • Baby Spider prefab, used in the public GameObject slot of the Spitball script.

Uses the following components; Transform; Rigidbody; Sphere Collider; Capsule Collider; Nav Mesh Agent; Animation; Animator; Audio Source; Enemy AI script (health, dmg & movement)

The Problem:

I have an enemy that ‘spits’ out a set of 5 eggs, which on collision with the terrain are supposed to spawn a baby spider at each egg. This is part is working, however as soon as the eggs are destroyed the baby spiders are instantly changing their Transform to 0, 0, 0 (the base Transform of the baby spider prefab).

Example of the problem:

Enemy spits egg > egg hits ground/terrain > baby spider instantiates > egg is destroyed > baby spider Transforms to 0, 0, 0.

The Code [C#] (current version) :

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

public class Spitball : MonoBehaviour {
    public Rigidbody spider;			// Reference to the spider RigidBody to be spawned
    public Transform spawnLoc;		// Reference to the Transform where the spider should spawn
    private bool collided;
    
    	void Update(){
    		if(collided == true){
    			// Show in debug the transform.position of the collision
    			Debug.Log ("Colliding at" + transform.position);
    
    			// Set the GameObject spider and Instantiate the spid at the eggs's transform.position
    			Instantiate (spider, spawnLoc.position ,Quaternion.identity);
    
    			StartCoroutine ("Pop");
    		}
    	}
    
    	void OnCollisionEnter(Collision other){
    		if(other.gameObject.tag != "Terrain"){
    			collided = true;
    		}
    	}
    
    	IEnumerator Pop(){
    		yield return new WaitForSeconds (0.2f);
    			// Destroy the egg
    		Destroy(spawnLoc.gameObject);
    	}
    }

The Code [C#] (older version) :

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

 public class Spitball : MonoBehaviour {
  public GameObject spider;			// Reference to the spider GameObject to be spawned
  public Transform spawnLoc;		// Reference to the Transform where the spider should spawn
    
    	void OnCollisionEnter(Collision other){
    		if(other.gameObject.tag != "Terrain"){
    			// Show in debug the transform.position of the collision
    			Debug.Log ("Colliding at" + transform.position);
    
    			// Set the GameObject spider and Instantiate the spid at the eggs's transform.position
    			Instantiate (spider, spawnLoc.transform.position ,Quaternion.identity);
    		}
    	}    
    }

The current version (shown first) allows the spiders to spawn but as soon as the Pop coroutine is run and the egg is destroyed the spiders reset to their own transform.

Hopefully this is detailed enough and my thanks in advance to anyone with the patience to help me, a humble noob, to understand and remedy my issue.

Hi there! How is your spawnLoc assigned? If the spawnLoc transform is the transform of the Spider itself, then as soon as you destroy it Unity is going to add a new Transform component with the default position value of (0,0,0). This behaviour occurs because Unity won’t allow a gameobject to be without a transform. Let me know if this helps!

I totally understood your problem but from the code, I am unable to understand the role of the “spawnLoc” Transform variable in the current version. Whose transform is it storing?
By the way, since this script is attached to the egg which when collides with the terrain generates a spider, you can simply use “this.transform.location” in the second argument of the Instantiate call instead of the spawnLoc.position. I guess that should work.