- Home /
Respawning gameobject
private Rigidbody2D RB2D;
public float fallDelay; // The time before platform falls
public float RespawnDelay; // The time before platform respawns
public Transform Spawnpoint; // Objects spawnpoint
void Start()
{
RB2D = GetComponent<Rigidbody2D>();
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.collider.name == ("Player"))
{
StartCoroutine(Fall());
}
}
IEnumerator Fall()
{
yield return new WaitForSeconds(fallDelay); // Wait platform to fall
RB2D.isKinematic = false; // Not kinematic so it falls
GetComponent<Collider2D>().isTrigger = true; // Is trigger now so won't collide
Destroy(RB2D.gameObject, 12); // Destroy object after set time
yield return new WaitForSeconds(RespawnDelay); // Wait respawn delay
RB2D.isKinematic = true; // Kinematic again so it won't fall
GetComponent<Collider2D>().isTrigger = false; // Is trigger again so collions apply
Instantiate(RB2D, Spawnpoint.position, Spawnpoint.rotation);// Instantiate to the Spawnpoint
yield return 0;
}
What it does: There is platform on scene that when player is colliding with it, it soon falls and after set time it respawns to the spawnpoint and then is destroyed after player again collides with it.
It currently works quite nice BUT when it creates new instances, it makes clones of itself so finaly there is like "gameobject(clonecloneclone...)". Just looks stupid and there surely is better way.
Also, now I must wait before destroying the gameobject or else I can't spawn it anymore because it doesn't exists.
So, how could I make the code better? I read about disabling it only and not destroying it but couldn't get it to work...
While I copied parts of it from web, I made it also suit better my needs, and since I'm still quite new to coding I could had surely made it better. Any thoughts of it overall all welcome.
Answer by thaMorganic · Apr 20, 2017 at 08:04 AM
Why destroy the game object when you can just move it to the spawn point?
Use this instead of the Destroy line.
RB2D.transform.position = Spawnpoint.transform.position;
Was thinking it way to hard and didn't notice that. I should be way over that allready... Now my code looks like the following:
yield return new WaitForSeconds(fallDelay);
RB2D.is$$anonymous$$inematic = false;
GetComponent<Collider2D>().isTrigger = true;
yield return new WaitForSeconds(respawnDelay);
RB2D.is$$anonymous$$inematic = true;
GetComponent<Collider2D>().isTrigger = false;
RB2D.transform.position = Spawnpoint.transform.position;
yield return 0;
After setting RB2D.is$$anonymous$$inematic = true; again, I would assume it should stop but as it seems it just keep falling faster and faster due gravity. Ain't that supposed to set it so that it WON'T be affected by gravity?
So currently it: falls normally and won't collide with anything --> spawns back on it's position but it keep the speed it has gained from falling. How to stop it?
Answer by Dr_ZIZO · Apr 21, 2017 at 09:13 AM
This is bad for performance especially on mobile devices. Anyway there's a much better way that doesn't have the heavy "instantiate " : Stack.
You can declare a Stack and put your objects to it and recycle your objects instead of instantiating them every time.
The "clone" problem is really easy. After you've declared your Stack , you can declare a name to all the clones.
If your Stack name is RBs , then you can write:
RBs.peek().name = "RB";