Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by lilBernier · Sep 12, 2015 at 04:20 PM · unity 5collidercollidersfpscontroller

need help with my health script

 var health = 300;
 
 function Update ()
 {
     
 }
 
 
 function OnCollisionEnter (Hit : Collision)
 {
     if (Hit.transform.gameObject.tag == "Zombies")
         health -= 20;
         
     if(health == 0)
      {
          Application.LoadLevel("Game Over Screen");
      }
         
 }
 
 function OnGUI() 
 {
     GUI.Box(Rect(20,20,health,20), "Health");
 }

its not giving me any compiler errors its just i have my zombies tagged exactly as Zombies just my character doesnt like getting killed or something it was working just fine earlier idk what happened i didnt even touch the script? or you could just help me make a new one or somethin idk

Comment
Add comment · Show 4
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 hexagonius · Sep 12, 2015 at 08:48 PM 0
Share

if you didn't touch the script the problem must be somewhere else. have you changed collider types, our the way all your entities move?make sure the script really works by putting a

 Debug.Log(Hit.collider.tag);

to it

avatar image lilBernier hexagonius · Sep 12, 2015 at 10:34 PM 0
Share

i put the debug in and indeed it is not working? and nothing is changed for colliders

avatar image lilBernier lilBernier · Sep 12, 2015 at 10:38 PM 0
Share

I added another capsule collider to my character and it started working? idek what was going on

avatar image joshf26 · Sep 12, 2015 at 09:35 PM 0
Share

It's possible that either your Zombie or Player's collider has been set as a trigger. This would make it not call the OnCollisionEnter() function. Check to make sure that the "Is Trigger" parameter in the inspector is unchecked on both your Zombie and Player.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tony_T · Sep 14, 2015 at 09:23 AM

You need to provide more information in order for us to help you. What do you want to achieve with this script? Now, there are a few wrong things with your script. First of all you are checking if the health is 0 in the collision enter function which is wrong. You need to check that in the update function, so it checks the health every frame. Also you never give the minimum limit for the health, so it can go below 0 (-1, -2, etc.). I did some changes to your script, try it and let me know (I'm not home to test it myself).

 var health = 300;
 
 function Update ()
 {
 //Checking if health is below zero.
     if (health < 0)
     {
        //If it is, make it zero.
     health = 0;
     }

     if (health == 0)
     {     
     Application.LoadLevel("Game Over Screen");
     }
     }
  
 function OnCollisionEnter (Hit : Collision)
 {
     if (Hit.transform.gameObject.tag == "Zombies")
     {
     health -= 20;
     }
 }
 
 function OnGUI() 
 {
 GUI.Box(Rect(20,20,health,20), "Health");
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

NEED HELP!!! How to explode my enemy ship? Need help with C# script 1 Answer

Collision problems 0 Answers

C# OnTriggerEnter Issue 3 Answers

URGENT!!! Colliders don't respond/move with imported Maya animation? 3 Answers

How do you do this? Help with prefabs. 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