- Home /
How do I make it so it spawns 1 clone instead of 2?
I'm making a 2d platformer and I got a death and respawn mechanic but when I die it makes 2 clones instead of just one which means he multiplies every time he dies, while it is a cool thing to have in a game it gets bad quick and doesn't fit my vision of the game.
Level manager script:
public class LevelManager : MonoBehaviour{
public static LevelManager instance;
public Transform respawnPoint;
public GameObject Chip;
private void Awake() {
instance = this;
}
public void Respawn () {
Instantiate(Chip, respawnPoint.position, Quaternion.identity);
}
}
Death:
public class DeathScript : MonoBehaviour
{
private void OnCollisionEnter2D(Collision2D collision) {
if (collision.gameObject.CompareTag("Spikes")){
Destroy(gameObject);
LevelManager.instance.Respawn();
}
}
}
Answer by Martin_Anderson · Jan 28 at 06:49 PM
It is possible that the script is running twice. Do you have the script on more than one object, or on an object more than once?
I checked and I didn't see it running twice, thanks for the help though!
Answer by privatecontractor · Jan 28 at 09:22 PM
Hi man,
I rewrite my answer in order to try help instead of making bigger mess in answers... Please be aware that have hard time going through your code... maybe because its displayed as plain text. So in the future (or maybe even start with this one... use edit options to post code).
Few things: you do not need:
public static instance LevelSomething....
lots of your mess occurs from fact that you cant check what and when accessing data/voids because you making everything public.... so according to your way, you make it public static instance to reach it from everywhere... to make it simple and easy.... but as same time, you will be not sure who call it, especially on bigger projects.
Instead push reference to this LevelManager through code, make Setup void on your Instantiate prefabs and push through it all references that need to be added.
Awesome, thx for fixing way of code being displayed.... This line:
LevelManager.instance.Respawn();
I belive it possible that your prefab making two times:
OnCollisionEnter2D
Before destroys itself?
Maybe try add at top not in void()...
bool m_calledForRespawn = false;
Then:
if (m_calledForRespawn) return;
m_calledForRespawn = true;
Destroy(gameObject);
LevelManager.instance.Respawn();
Hope now will do, let me know.
My problem with the first one is that I need the levelmanager instance public for the death script and when I added the debug log it told me thousands of times how many clones are added each time with the name "audio listener instead of clone"
Your answer
Follow this Question
Related Questions
How to make a companion that trails character moves, and not collide? 0 Answers
I cant get wall jumping to work in my 2D platformer (C#) 1 Answer
Why does my character pass through walls when he respawns and I lose control of him? 2 Answers
error CS1525: unexpected symbol. How do I fix this and why am I getting this error? 1 Answer
How do I make the enemy script/AI stop following me when It is in view of the player camera? 1 Answer