• 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
Question by sandhillceltic · Mar 13, 2013 at 11:31 PM · javascriptcollisionhealth

Problem with health and level reload system

So I am making a game, and I cannot figure how to make a basic collision system. I want the player lose health when they collide with a certain GameObject, and reload the level after a certain number of $$anonymous$$ts. The health and damage should be editable, along with the GameObject. T$$anonymous$$s is the way I did it-

 var Enemy : GameObject;
 var health = 10;
 var damage = 2;
 
 function Update(){                                             //what to do with a function inside a function?
   function OnCollisionEnter(collision:Collision) {
     if(collision.gameObject.name = Enemy){                          //refering to the var
         health -= damage;
     }
     
     if(health < 1){
       Application.LoadLevel(Application.LoadedLevel);             //I t$$anonymous$$nk that I have the right syntax for t$$anonymous$$s....
     }
   }
 }
 //how would I add a $$anonymous$$tscreen via GUI text?
 //some very interesting complier errors
 //thanks in advance, I'm new, and need all the help I can get

I need the answer urgently, can someone look over the code and find my mistakes? -Thanks in advance

Comment
Khada

People who like this

-1 Show 5
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 sandhillceltic · Mar 14, 2013 at 02:01 AM 0
Share

A trimmed down version of the code, that compiles properly, but does not work.

 var health = 5;
 var damage = 1;
 
 function OnCollisionEnter(collision:Collision) {
   if(collision.gameObject.tag == "Enemy"){  
     health -= damage;
   }
     
   if(health < 1){
     Application.LoadLevel(Application.LoadedLevel);         
   }
 }
 
avatar image Khada · Mar 14, 2013 at 02:09 AM 0
Share

How doesn't it work? Throw some Debug.Log calls in there and see what is/isn't being called etc.

avatar image sandhillceltic · Mar 14, 2013 at 02:13 AM 0
Share

I just spoke to one of my friends that knows quite about Unity. He said that since the game is first person, the FP controller is not considered a proper collider. I assume that means I have to add a capsual collider to replace it...

avatar image Khada · Mar 14, 2013 at 03:06 AM 0
Share

I've never bothered with unity's controllers but from what I've seen of them, that sounds correct.

avatar image Khada · Mar 15, 2013 at 02:07 AM 0
Share

Was my answer correct? If so can you mark it by clicking on the gray tick icon (under the thumbs down icon). Otherwise you can ask for more help or even post the answer yourself. This helps keep the site clean.

I also see that this hasn't been done for all 9 of your questions. Can you please take a moment to give credit, where due, to the volunteers who give their time to help? Thanks.

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Khada · Mar 14, 2013 at 12:33 AM

Issues I can see:

(1) Variable name Enemy should be changed to enemy. All variables should begin with a lower case letter, w$$anonymous$$le all methods/types should begin with an upper case letter to prevent confusion or conflicts in the code.

(2) The OnCollisionEnter method should not exist wit$$anonymous$$n another function. I'd be very surprised if that script even compiles. Put all new functions inside the class but outside other functions. Edit: Or just outside other methods if you are in javascript.

(3) You're comparing an object with an object name on line 7. Instead you want to check an object name against and object name. You're also using the assignment operator (a single '='), not the comparison operator (double '='). Thus line 7 should read:

 if(collision.gameObject.name == enemy.name)
Comment
sandhillceltic

People who like this

1 Show 0 · 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
Wiki

Answer by DryTear · Mar 15, 2013 at 02:23 AM

instead of "OnCollider" use "OnControllerColliderHit", also do "Debug.Log("ItWorks");" to check wheres the error. Enemy should me non-capital, infact heres a script - attach it to the enemy:

 function OnTriggerEnter(x : Collider)
 {
     if(x.gameObject.tag == "Player")
     {
         x.gameObject.SendMessage("GotDamaged");
     }
 }

^^^ Attach to enemy, create an empty game object and attach a collide with isTrigger to true to the player, then t$$anonymous$$s script onto the player:

 var health : float;
 var damage : float;

 function GotDamaged()
 {
     health -= damage;
 }

correct me if im wrong

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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 do basic health & damage on collision 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

How do I delete an object on collision with another object? 1 Answer

I have a problem with implementing a health and death system, help? 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