Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by shay4545 · Jan 25, 2015 at 12:21 AM · c#androidscripting problemprefabenemy

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges