- Home /
Instantiate creates endless clones and disables collider
Hi,
I'm working on a multiplayer topdown shooter in Unity2D. I duplicated the first Player to have a second controllable Character. Moving, shooting and "killing" each other works fine. Now I wanted to implement a Respawn. Whenever a character is killed, it should respawn after a certain time at the position the character was killed. I tried to do so with the following script (in the HealthScript, attached to every Player):
public void Damage(int damageCount)
{
hp -= damageCount;
if (hp <= 0)
{
Destroy(gameObject);
Instantiate(gameObject);
}
}
The problem is that Unity creates a clone of the player (and names it "Player (clone)") and disables the BoxCollider2D. The bullets fly right through this character and only do damage if I enable the BoxCollider2D manually in the inspector of this clone. How do I solve this problem?
(Another Problem is that the health of this clone is at 0, instead of maxhealth of 10.)
Thanks in advance!
Answer by The_Toller · Jun 11, 2015 at 10:46 AM
Hello!
You're trying to instantiate an object you've just destroyed. I can't explain the process of destroying a GameObject but I see why you have trouble with it. When destroying, you're effectively removing the object from your game and since the script is attached to it, I'm surprised you get the Instantiate call running at all. I'm not going to try figuring that out, I would undoubtedly give a load of wrong and bad information.
However, I can give you another solution which would solve your problem. First of all, Instantiating and Destroying objects have their use, but since your goal is to kill and revive essentially the same object, you should really consider reusing the object you already have. Instead of destroying your object, you should just disable it. And then after a period of time, re-enable it and reset its health to the maximum value. Here's an example:
public class PlayerRespawn : MonoBehaviour
{
public int hp; //Current hit points
public int maxHP = 10; //Maximum hit points
public float respawnTime = 10f; //Time to respawn
public void Damage(int damageCount) //Get a damage value
{
hp -= damageCount; //Subtract damage value from hit points
if(hp <= 0) //Check to see if hit points are 0 or bellow
{
gameObject.SetActive(false); //Disable this gameObject
Invoke("Respawn", respawnTime); //Call the method called "Respawn",
}
}
void Respawn() //Respawn method
{
gameObject.SetActive(true); //Activate the gameObject
}
void OnEnable() //Method that runs when the gameObject is activated
{
hp = maxHP; //Set the hit points to maximum value
}
}
Chiao!
Note: The OnEnable method is also called when the object is created the first time. That's why I didn't, and don't need - to set the hp to an initial value. It will automatically start with maximum hit points!
Please mark my answer as the answer if it helped you. :)
Your answer
Follow this Question
Related Questions
Respawn Player Using a Coroutine 0 Answers
Respawn Management 2 Answers
Player Respawn After Death 1 Answer
loss of life help 1 Answer
My object don't spawn as expected so what should I do? 0 Answers