0

I am facing a problem in Unity3D. I have the same health script attached to both the player and the enemy. I want to show the game-over message when the player dies but the issue is that the game over message appears for both the player and enemy when they die.

My code looks like is that:

public class CharacterStats : MonoBehaviour 
{
    // Use this for initialization
    void Start () 
    {
    }

    // Update is called once per frame
    void Update () 
    {
        health = Mathf.Clamp (health, 0, 100);
    }

    public void damage(float damage)
    {
        health -= damage;
        if(health<=0)
        {
            Die();
            Application.LoadLevel(gameover);
        }
    }

    void Die()
    {
        characterController.enabled = false;
        if (scriptstodisable.Length == 0)
            return;
                 foreach (MonoBehaviour scripts in scriptstodisable)

                scripts.enabled = false;
                 if (ragdollmanger != null)
                     ragdollmanger.Raggdoll();
    }
}
1
  • You may add an enum which describes type of your character (player or enemy) and show 'die' message only if your enum's value == player Commented Dec 11, 2017 at 10:08

2 Answers 2

2

As you are using 1 script for both player and enemy. You should have different classes for both and implement an interface or derive from a base class to implement health:

public class Character : MonoBehaviour 
{
    public float Health;

    public virtual void Damage(float damageValue) 
    {
        Health -= damageValue;
    }

    public virtual void Die()
    {

    }
}

public Enemy : Character
{
    public override void Die()
    {
        // do enemy related stuff
    }
}

public Player : Character
{   
    public override void Die()
    {
        // do player related stuff.
        // like game over screen
    }
}

Hope this helps :)

Sign up to request clarification or add additional context in comments.

Comments

1

You could use a bool to check whether CharacterStats is attached to the player, for example by adding a tag called ”Player” to the player game object and checking if gameObject.tag == “Player”, or you could equivalently name the game object ”Player” and check gameObject.name if you so wish.

You could then run the function for the game over message only if the game object is a player (isPlayer is true).

public class CharacterStats : MonoBehaviour 
{
    bool isPlayer = false;

    // Use this for initialization
    void Start () 
    {
        if(gameObject.tag == “Player”) 
        {
            isPlayer = true;
        }      
    }

    // Update is called once per frame
    void Update () 
    {
        health = Mathf.Clamp (health, 0, 100);
    }

    public void damage(float damage)
    {
        health -= damage;
        if(health<=0)
        {
            if(isPlayer)
            {
                // Do Player-only stuff
            }

            // Do Stuff for both players and enemies
        }
    }
}

2 Comments

The tag is a string and should be in double quotes like this: if(gameObject.tag == "Player").
Thanks @padonald. Was a silly typo. Did it work for you now, @yasirkhan?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.