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 Kenshen112 · Apr 09, 2013 at 09:59 PM · errorhealth

Sorry Me again :P Error CS0131

So i need more help with my health script I'm trying to add a game over
Player Health using UnityEngine; using System.Collections;

 public class PlayerHealth : MonoBehaviour {
     // Use this for initialization
     int Health = 100;
     public void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     if (Health == 0)
         {
         Application.LoadLevel("GameOver");
         }
     }
     
     void OnGUI () {    
     }
     public void ChangeHealth(int howMuch){
     this.Health += howMuch;
     }
 }

Enemy

 using UnityEngine;
 using System.Collections;
 
 public class Enemy : MonoBehaviour {
 
     // Use this for initialization
     public void OnCollisionEnter(Collision collision)
     {     
         PlayerHealth playerHealth = GetComponent<PlayerHealth>();
         if (collision.gameObject.tag == "Player")
         {
             if(playerHealth == null) return; // Get out.
             while(playerHealth.ChangeHealth = 100)
             {    
                 playerHealth.ChangeHealth(-10);
             }
     }
     }
     // Update is called once per frame
     void Update () {
      
     }
 }

In my Enemy script I've added a while loop to Loop health damage each time i collide with Enemy object in player health game over condition If i ask a lot of questions here I'm sorry I've always learn from experience any help would be appreciated :).

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Dracorat · Apr 09, 2013 at 10:05 PM

Player Health

 using UnityEngine; 
 using System.Collections;
 
 public class PlayerHealth : MonoBehaviour {
     int Health = 100;

     public void ChangeHealth(int howMuch){
         this.Health += howMuch;
         // Are we dead?
         this.CheckForDead();
     }

     //For everything you might want to do with PlayerHealth, you probably want to have 
     // an associated function (method)
     private void CheckForDead(){
          if(this.Health < 1) {
              Application.LoadLevel("GameOver");
          }
     }
 }

Enemy

 using UnityEngine;
 using System.Collections;
  
 public class Enemy : MonoBehaviour {
  
     // Use this for initialization
     public void OnCollisionEnter(Collision collision)
     {     
        PlayerHealth playerHealth = GetComponent<PlayerHealth>();
        if(playerHealth == null) return; // Get out.

        if (collision.gameObject.tag == "Player")
         {
           playerHealth.ChangeHealth(-10);
           // Logic like "Is the player dead?" doesn't go here. That's up to the PlayerHealth class to decide.
           // Also, your previous while loop would only loop once because it would only loop if the playerhealth was equal to 100 which makes no sense.
         }
     }

     //You don't need to keep around the default methods (like Update) if you aren't using them.
 }
Comment
Add comment · Show 3 · 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 Chronos-L · Apr 10, 2013 at 01:59 AM 0
Share

Addition:

 //I don't think you have PlayerHealth for your enemy
 PlayerHealth playerHealth = GetComponent<PlayerHealth>();
 
 //Correction
 PlayerHealth playerHealth = collision.gameObject.GetComponent<PlayerHealth>();
avatar image Kenshen112 · Apr 11, 2013 at 07:48 PM 0
Share

Can you help me? my collision detection doesn't work my player (Capsule) is a rigidbody and my capsule (Enemy) is a normal Collider nothing so i made my enemy a rigidbody and deleted the capsule Collider but the enemy now falls through the terrain floor

avatar image Dracorat · Apr 11, 2013 at 07:50 PM 1
Share

That's a separate question and you should post it as such.

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

Need help calling a variable from another C# script 1 Answer

Calling a variable from another class 2 Answers

Variable from other class always returns 0 1 Answer

Critical problem in importing scripts 3 Answers

Setting up health script. will not work, 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