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 noggy_yo · May 03, 2012 at 05:21 AM · collisionplayerdestroydetection

collision wont work

im making a game where the player looses energy every time they move and in order to regain energy to have to find wells and collect water.

there are enemies who are trying to catch you. ive written the code up to this point but im very new to unity and i cant get the collision to work.

i want it so that if an enemy hits you, you die (destroy player) and if you hit the well, the water will destroy but your energy will be replenished.

i have objects with tags : Player. enemy, water and well

please help im desperate! the code is in C#:

 using UnityEngine;
 using System.Collections;
 
 public class player_health : MonoBehaviour {
     public int maxHealth = 100; // maximum health
     public int curHealth = 100; // current health
     public float healthBarLength;
     object Player = (GameObject) GameObject.FindWithTag("player");
     object Enemy = (GameObject) GameObject.FindWithTag("enemy");
    
     // Use this for initialization
     void start() {
         healthBarLength = Screen.width / 2;                
     }
 
     // Update is called once per frame
     void Update () {
     
     addjustCurrentHealth(0);
     gameOver();             
     }
        
     void OnGUI() {
         GUI.Box (new Rect(10, 10, Screen.width / 2 / (maxHealth / curHealth), 20),curHealth + "/" + maxHealth) ;
         
             if(curHealth < 2){
             
             GUI.Label(new Rect(10,10,Screen.width, Screen.height), "Nearly one in five children die before their fifth birthday in Somalia. This is the world’s highest mortality rate for children. Every child deserves the right to live with access to clean water and without fear of danger. You can make a difference by donating to the Unicef East Africa Emergency Appeal. press enter to restart or press space to visit the website");
             
                 if(Input.GetKey(KeyCode.Space))
                 {
                     Application.OpenURL("http://www.unicef.org.au/Donate/One-off-Donation/east-africa-drought-emergency-appeal-famine.aspx");
                 }    
         }}
           
     public void addjustCurrentHealth(int adj) {
         curHealth += adj;
         
         if(curHealth < 1)
             //curHealth = 1;
         if(curHealth > maxHealth)
             curHealth = maxHealth;
             
         if(Input.GetKey (KeyCode.LeftArrow))
             curHealth = curHealth - 1 ;
             if(Input.GetKey (KeyCode.RightArrow))
             curHealth = curHealth -1;
         
         if(Input.GetKey (KeyCode.UpArrow))
             curHealth = curHealth - 1;
         if(Input.GetKey (KeyCode.DownArrow))
             curHealth = curHealth - 1;                      
     }
       
     public void gameOver() {
     
           if (Input.GetKeyDown (KeyCode.Return)) {  
     Application.LoadLevel (0);  
   }            
    }        
     
 void onTriggerEnter(BoxCollider other){
         
     if(other.tag == "enemy"){       
             curHealth -= 50;
             Destroy(GameObject.FindWithTag("Player"));          
     }
     
     if(other.tag == "well"){
             Destroy(GameObject.FindWithTag("water"));
                 }
     }   
 }    
     
 
 
         
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 fafase · May 03, 2012 at 05:31 AM 0
Share

I kinda squeeze your code a little, for later posts try to only provide relevant parts, only the collision part is useful from the 50 lines you posted here.

avatar image noggy_yo · May 03, 2012 at 05:40 AM 0
Share

sure, sorry

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · May 03, 2012 at 05:27 AM

So you want the well to give you health and the enemy to kill you:

 void OnTriggerEnter(Collider other){ // Here you were using the wrong class, Collider is needed, also check the Cap letters
 
   if(other.tag == "enemy"){
     curHealth -= 50;    // this line not so useful since you destroy the player right after 
     Destroy(gameObject);
   }
   if(other.tag == "well"){
     curHealth += 50;       
     Destroy(other.gameObject);
   }
 }

Try and say if any luck, that should though.

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

Collision Detection Issue with SetActive(false) not working 2 Answers

Collision Detection for a Prefab? 3 Answers

Collision Detection between certain objects 2 Answers

Collision Detection not working? 1 Answer

Collision Detection When Picking Up GameObjects 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