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 Vmadmax · Aug 29, 2012 at 12:45 AM · enemylife

Enemy health

Hey, i have a problem with my enemy script. i didn't found the solution in similar topics. I have 2 enemies (dublicated). My problem is, that the life of both is decreasing simultaneously. Do you know why?

1. Enemyscript

 public static var Health = 100;
 
 static function Damage(){
     if(Health > 0){
         Health -= Cannon.myDamage;
         Debug.Log(Health);
     }
     
 }


2. Cannonscript

var mySpeed : float = 10; var myRange : float = 10; public static var myDamage : float = 5; private var myDist : float; var counter = 1;

 static var testname;
 
 function Update () 
 {
      transform.Translate(Vector3.forward * Time.deltaTime * mySpeed);
      myDist += Time.deltaTime * mySpeed;
      if(myDist >= myRange){
         Destroy(gameObject);
         }
 
 
 }
 
 function OnCollisionEnter(collision: Collision)
 {
     if(collision.gameObject.tag == "Enemy")
     {
         if(collision.gameObject.name == "Enemy!"){
         
         
         if(collision.gameObject.GetComponent(CubeEnemy).Health > 0){
 
                      
         collision.gameObject.GetComponent(CubeEnemy).Damage();}
         
     
     
         if(collision.gameObject.GetComponent(CubeEnemy).Health <= 0){
         
             Destroy (collision.gameObject);    
                 
                     
         }
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

4 Replies

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

Answer by Piflik · Aug 29, 2012 at 01:00 AM

Because your health variable is static. There is only ever one instance of a static variablein the scene, and when two or more objects use it, they are always both affected at the same time. Use normal, public variables for your enemies' health instead. Also rhe static nature of the function might give you problems.

Use static variables/functions only, if they are used uniquely, for example the player's health.

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 kristercollin · Aug 29, 2012 at 01:35 AM 0
Share

Philipp's right, you've made the health variable global, which means that all gameobjects in the scene can access it, and both of these objects ARE accessing it, so they can both make changes to it that will reflect on the other. What you want to do is make it a local variable so that it only affects (and can be easily affected by) the gameobject it belongs to.

So, you would want to change:

"public static var Health = 100;"

to

"public int Health = 100;"

Then, each gameobject with this script would have it's own "Health" attribute.

avatar image
0

Answer by Dasherz · Aug 29, 2012 at 12:55 AM

if they both have the same health variable "health" then both their health is going to go down at the same time.

make 2 different variables

enemyHealthOne enemyHealthTwo

also within your tag you will have to define which enemy you are hitting.

eg

  if(collision.gameObject.tag == "Enemy")
     {
        if(collision.gameObject.name == "enemyOne"){
 enemyHealthOne --;
 ......
 
 else  if(collision.gameObject.tag == "Enemy")
     {
        if(collision.gameObject.name == "enemyTwo"){
 enemyHealthTwo --;
 ......

Hope this helps

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 Piflik · Aug 29, 2012 at 01:09 AM 0
Share

While this might work for two enemies, it will get extremely complicated with multiple enemies and is not practicable. Gladly it is also completely unnecessary, because each instance of an object uses its own set of variables, as long as they are not static.

avatar image
0

Answer by Sundar · Aug 29, 2012 at 12:56 AM

try this

 static function Damage(){
     if(this.Health > 0){
        this.Health -= Cannon.myDamage;
        Debug.Log(this.Health);
     }
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 Piflik · Aug 29, 2012 at 01:16 AM 0
Share

Don't use statc functions when you want to access non-static data (which Health isn't, but you treat it like it was). It won't work.

Also this.Health will not produce anything usefull, because Health is still static and static variables aren't attributable to any object.

avatar image
0

Answer by Vmadmax · Aug 29, 2012 at 10:38 AM

Thank you very much! I now understand what I did wrong. I works perfectly

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

12 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

Related Questions

How to Stop Enemy "Shooting Through A Wall" 1 Answer

Need help with AI in multiplayer 1 Answer

how can i destroy a cube when i press a gui button??? 1 Answer

How to make enemy run animation 3 Answers

Script to make an enemy follow the player when they trigger a door? 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