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 Flati · Oct 11, 2011 at 10:03 AM · collidervariables

Accessing variables in enemy.

I'm making a tower defense game. The problem I'm having is that whenever a bullet hits, all the enemies lose hp instead of just the one collided with by the bullet. The first mob in each wave is normal, but when it is dead, every mob after him has 0 hp or less and will be killed as soon as a bullet hits them.

Is there a way to access the static variable in the enemy collider without accessing the script. (I'm guessing the problem is that it's accessing the script and reducing hp by every gameObject that has that type.)

This is the function in bullet script:

 var bulletSpeed : int;
 var lifeTime : float;
 var damage : int;
 
 function Start () {
     lifeTime = Time.time;
 }
 
 function Update () {
     transform.Translate(Vector3(0,0,bulletSpeed));
     if (lifeTime + 0.5f < Time.time)
     {
         Destroy(this.gameObject);
         LifeLostScript.enemies--;
     }
 }
 
 function OnTriggerEnter (collider : Collider) {
     if (collider.tag == "Enemy")
     {
         collider.GetComponent(EnemyMovementScript).hp -= damage;
         if(collider.GetComponent(EnemyMovementScript).hp <= 0)
         {
             Destroy(collider.gameObject);
         }
         Destroy(this.gameObject);
     }
 }

This is the enemyscript:

 var speed : int;
 var HP : int;
 static var hp : int;
 
 function Start () {
     hp = HP;
 }
 
 function Update () {
     transform.Translate(Vector3(speed*Time.deltaTime, 0, 0));
 }
 
 function OnTriggerEnter (collider : Collider){
     if (collider.tag == "RightRotation") {
         transform.Rotate(0,90,0);
     }
     if (collider.tag == "LeftRotation") {
         transform.Rotate(0,-90,0);
     }
 }
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

2 Replies

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

Answer by syclamoth · Oct 11, 2011 at 10:12 AM

Simple- don't use static variables! The problem with static variables is that they are not linked to any specific instance- they are global values across every instance of that class. In your case, you probably want to just swap around HP and hp- seeing as you seem to be using one where you should have been using the other. Also, you should set the global one to some number right at the top in the variable designation-

 static var HP : int = 100;
 var hp : int;

then in Start,

 function Start () {
     hp = HP;
 }

And all the rest of it remains basically the same.

Comment
Add comment · Show 1 · 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
avatar image Flati · Oct 11, 2011 at 10:33 AM 0
Share

worked perfectly, thank you

avatar image
0

Answer by aldonaletto · Oct 11, 2011 at 10:07 AM

Remove the static keyword before hp - static vars are unique: even if you have several script instances running at the same time, only one static hp will exist, and everybody will read and write the same variable.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Easily cleaning up \ deleting unused variables? 2 Answers

updating variable of one script in other 1 Answer

animation and sound on collide 1 Answer

Take boolean from other script error 1 Answer

Trying to access a variable from another script attached to same object. 1 Answer


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