- Home /
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 :).
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.
}
Addition:
//I don't think you have PlayerHealth for your enemy
PlayerHealth playerHealth = GetComponent<PlayerHealth>();
//Correction
PlayerHealth playerHealth = collision.gameObject.GetComponent<PlayerHealth>();
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
That's a separate question and you should post it as such.
Your answer
![](https://koobas.hobune.stream/wayback/20220613100016im_/https://answers.unity.com/themes/thub/images/avi.jpg)