- Home /
All enemies die when one does
I'm having a problem with the scripting in my game. When I jump on one enemy to kill it all the enemies die. They were all dragged into the scene view from a prefab. Here is the script on my enemies:
public float velocity = -3;
public Transform startCast;
public Transform endCast;
public bool GroundHit;
public bool OnGround;
public bool WallHit;
public Transform WallCheckStart;
public Transform WallCheckEnd;
public Transform GroundCheckStart;
public Transform GroundCheckEnd;
public LayerMask GroundLayer;
public LayerMask WallLayer;
public static bool dead, noMoreKillPlayer;
private GameObject me;
public BoxCollider2D Box1, Box2;
Rigidbody2D rg;
SpriteRenderer spritey;
IEnumerator KillSequence(){
if (PlayerControls.DoneKilling) {
rg.gravityScale = 0;
spritey.sortingLayerName = "EnemyBG";
Box1.enabled = false;
Box2.enabled = false;
yield return new WaitForSeconds (2f);
Destroy (me);
}
}
void Start () {
me = this.gameObject;
dead = false;
rg = GetComponent<Rigidbody2D> ();
spritey = GetComponent<SpriteRenderer> ();
}
void FixedUpdate () {
rigidbody2D.velocity = new Vector2 (velocity, rigidbody2D.velocity.y);
GroundHit = Physics2D.Linecast (startCast.position, endCast.position, GroundLayer);
OnGround = Physics2D.Linecast (GroundCheckStart.position, GroundCheckEnd.position, GroundLayer);
WallHit = Physics2D.Linecast (WallCheckStart.position, WallCheckEnd.position, WallLayer);
if (!GroundHit && OnGround || WallHit) {
transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
velocity *= -1;
}
if (dead) {
this.velocity = 0;
StartCoroutine(this.KillSequence());
}
}
This script is on the killbox which is a child of the enemy:
public GameObject me;
void OnTriggerEnter2D(Collider2D hit){
if (hit.tag == "Feet") {
SimpleEnemy.dead = true;
SimpleEnemy.noMoreKillPlayer = true;
}
}
void FixedUpdate(){
if (SimpleEnemy.dead == true && PlayerControls.DoneKilling == true) {
me.SetActive(false);
}
}
I think the problem is that the dead variable is being set true for everyone with the enemy script on which is all of the enemies. Would I have to distinguish which enemy I am jumping on in either the player or enemy script? If so, how would I do that?
Answer by Fornoreason1000 · Jan 25, 2015 at 12:44 AM
its because Dead is a static variable, this means it applies to all instances of Enemy all the time. try not making it static.
Your answer
Follow this Question
Related Questions
Tower defense target update 0 Answers
Help with getting a bool from another script 2 Answers
instantiate random prefabs based on player camera distance 1 Answer
If I duplicate enemies, will their scripts mess each other up? 2 Answers
Admob ad Problem 0 Answers