Hey all,
I’m attempting to respawn an object in it’s original spot once it’s destroyed. It seems to work the first time but it’s not working past that. I see what’s going on, for some reason the clone does not have the objects I’ve attached to the original objects script.
For a little context, I’m dragging this object onto a character. Once it hits him I want the object to be destroyed and a replacement to respawn in the objects original place.
Is there something I need to do to ensure the clones call these objects? I’ve attached my code below.
public GameObject fishFood; //= GameObject.Find ("Food");
public Transform fishFoodSpawn;
public Vector2 spawnLocation = new Vector3 (-3.88f, 2.44f, 0f);
private bool dragging = false;
private float distance;
void OnMouseDown()
{
distance = Vector2.Distance(transform.position, Camera.main.transform.position);
dragging = true;
}
void OnMouseUp()
{
dragging = false;
}
void Update()
{
if (dragging)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector2 rayPoint = ray.GetPoint(distance);
transform.position = rayPoint;
}
}
// Destroy the food once it enters the bears mouth area
void OnTriggerEnter2D (Collider2D other)
{
if (other.tag == "Bear")
Destroy (gameObject);
}
void OnDestroy() {
Instantiate (fishFood, spawnLocation, Quaternion.identity);
}
}
Thanks in advance!