0
votes

So look: I have a monster in the game that when player collides with him a panel appears and says try again. There is now a shield object, that will protect the player from death, so when the player collides with it and then if you touch the monster you shouldn't die for one time: it will be like that, you touch the shield and you are protected from one death. Can you help me write this code?

public GameObject panel;
public bool hasShield = false; /* no shield in the beginning */    

void OnCollisionEnter(Collision col)
{
    if(col.gameObject.tag == "Shield") 
    {
        hasShield = true; //We are safe now.
        /* TODO: StartCoroutine() or Invoke() to reset the variable and the graphic effect after some amount of time. */
    }
    else if (col.gameObject.tag == "Monster" && !hasShield) 
    { 
        //We hit a monster and had no shield. Display gameover.
        panel.SetActive (true);
    }
    //I assume the panel is inactive by default so no need to call SetActive(false) on it.
}
2
Can you be more specific than "doesn't work"? Have you done any debugging? - halfer
Can you describe your logic better? When exactly is the panel supposed to be activated or deactivated? - Maximilian Gerhardt
The panel needs to be activated after i touch the monster, but if i touch the shield and then the monster then the panel should not appear - Melinceanu Victor

2 Answers

5
votes

The inside of this code can never be reached:

     if (col.gameObject.tag == "Shield") 
    {
        if(col.gameObject.tag == "Monster")
        {
            panel.SetActive(false);
        }
    }

For the first if check to pass, the tag must be "Shield", however for the second test to pass, it must be "Monster". These two conditions cannot be true at the same time, the code is equivalent to

 if(col.gameObject.tag == "Shield" && col.gameObject.tag == "Monster")

However, if you replace && with || (logical OR) in the expression above, you will get your desired results. So, the rewritten code would be

    if (col.gameObject.tag == "Monster") 
    { 
        panel.SetActive (true);
    }
    if (col.gameObject.tag == "Shield" || col.gameObject.tag == "Monster") 
    {
         panel.SetActive(false);
    }

The inside code will now trigger if the is "Shield" or "Monster".

EDIT: As we've talked about it in the comments, the logic was flawed overall. Now according to your description

The panel needs to be activated after i touch the monster, but if i touch the shield and then the monster then the panel should not appear

I advise the following: When a shield is collected, save this to some variable, like

bool hasShield;

And set it to true when the shield is collected. Then, if you hit something, check if it is a monster AND if you don't have a shield, then you see the "Game Over" screen. So alltogether the player should look something like

    public GameObject panel;
    public bool hasShield = false; /* no shield in the beginning */    

    void OnCollisionEnter(Collision col)
    {
        if(col.gameObject.tag == "Shield") 
        {
             hasShield = true; //We are safe now.
             /* TODO: StartCoroutine() or Invoke() to reset the variable and the graphic effect after some amount of time. */
        }
        else if (col.gameObject.tag == "Monster") 
        { 
            if(!hasShield)
                panel.SetActive (true);  //We hit a monster and had no shield. Display gameover.

            else
                hasShield = false; //loose the shield
        }
        //I assume the panel is inactive by default so no need to call SetActive(false) on it.
    } 
0
votes

this is the corect code!

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class DestroyPlayerOnTouch : MonoBehaviour {
    public GameObject panel;
       bool hasshield = false;

    void OnCollisionEnter(Collision col)
    {
          if (col.gameObject.tag == "Monster"  && !hasshield) 
        { 
            panel.SetActive (true);
        }

        else if(col.gameObject.tag == "Monster" && hasshield)
        {
            panel.SetActive (false);
        }
    }

    void OnTriggerEnter(Collider other) 
    {
        if (other.gameObject.tag == "Shield") 
        {
            Debug.Log("hasshield!");
            hasshield = true;
        }
    }
}