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 conflictbliz · Jan 14, 2012 at 06:53 AM · hitpointshealth

hitpoints problem

i have 2 scripts one that applys damage on contact to the tagged object and the other is the health points and for some reason they are nto working i am using unity 3.5 beta. var damage : float;

 function OnCollisionEnter (other : Collision){
   if (other.transform.tag == "Enemy"){
     // Tell the object you hit that it was damaged
     other.transform.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
   }
 }

and

  var hitPoints = 100.0;   // this is the health; when <= 0 the object dies
 
 function ApplyDamage (damage : float) { 
     // We already have less than 0 hitpoints, maybe we got killed already?
     if (hitPoints <= 0.0) return;
 
     hitPoints -= damage;  // subtract damage from health
     if (hitPoints <= 0) {  // if health <= 0 the object goes to the grave:
         // Destroy ourselves 
         Destroy(gameObject);
 }
 }
Comment
Add comment · Show 2
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 adrenak · Jan 14, 2012 at 08:21 AM 0
Share

Where have you added the first script ? I think that this link might be of help, it seems to be a similar question:

http://answers.unity3d.com/questions/204197/damagescript.html

avatar image conflictbliz · Jan 14, 2012 at 07:10 PM 0
Share

it did not help because when i added the script it didn't know what hitpoint was so i changed it to damage and its the exact same script i have made, after i took out all of the errors.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by FLASHDENMARK · Jan 14, 2012 at 07:36 PM

 function OnCollisionEnter (other : Collision){
   if(other.gameObject.tag == "Enemy"){
     // Tell the object you hit that it was damaged
     other.gameObject.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
   }
 }

and

 var hitPoints = 100.0;   // this is the health; when <= 0 the object dies
 
     function ApplyDamage (damage : float) { 
         hitPoints -= damage;  // subtract damage from health
         if (hitPoints <= 0) {  // if health <= 0 the object goes to the grave:
            // Destroy ourselves 
            Destroy(gameObject);
         }
     }

I think your problem was: if (hitPoints return; if health is less or equal to 0 you are stopping the rest of your code of executing because you are returning and therefore you are not destroying your object.

Another problem. I think SendMessage is type of GameObject not transform. So I changed it to other.gameObject.SendMessage, not sure though, but better safe than sorry ;)

Comment
Add comment · Show 6 · 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 Thom Denick · Jan 14, 2012 at 08:01 PM 0
Share

I think Orange lightning is correct. Since subtracting HP isn't going to cause a crash or anything, you should just delete that check at the start altogether as he has done here.

avatar image conflictbliz · Jan 14, 2012 at 10:24 PM 0
Share

i tried your scripts but it doesn't work i put the first one on my bullet i launch at it, and the second script on the alien. Am i doing this wrong? because i tried it with my own health script and it did not work here is what i used

var hitpoints : float;

function Awake (){ hitpoints = 100;
}

avatar image FLASHDENMARK · Jan 14, 2012 at 11:34 PM 0
Share

@conflictbliz what type of controller is your alien? Is it a rigidbody, CharacterController or neither?

avatar image conflictbliz · Jan 15, 2012 at 03:36 AM 0
Share

it is just a model with a box collider i added a rigidbody and it still did not work, why does it need some sort of AI to work?

avatar image conflictbliz · Jan 26, 2012 at 08:33 PM 0
Share

change it abit and i get this error

$$anonymous$$issing$$anonymous$$ethodException: The best match for method ApplyDamage has some invalid parameter. System.$$anonymous$$onoType.Invoke$$anonymous$$ember (System.String name, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object target, System.Object[] args, System.Reflection.Parameter$$anonymous$$odifier[] modifiers, System.Globalization.CultureInfo culture, System.String[] namedParameters) UnityEngine.SetupCoroutine.Invoke$$anonymous$$ember (System.Object behaviour, System.String name, System.Object variable) (at C:/BuildAgent/work/842f9557127e852/Runtime/Export/Coroutines.cs:18) UnityEngine.GameObject:Send$$anonymous$$essage(String, Object, Send$$anonymous$$essageOptions) Hitpoints:OnCollisionEnter(Collision) (at Assets/Hitpoints.js:4)

Show more comments

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

7 People are following this question.

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

Related Questions

[SOLVED]cant load lvl and i cannot figure out why. 1 Answer

Health C# script problem. 3 Answers

Expecting ) found = 1 Answer

health problem 1 Answer

How do i set health to regenerate over a specified range of time? 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