Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Defeat a enemy and victory message to appear, with trouble

Discussion in 'Scripting' started by FoxHound9001, Mar 28, 2021.

  1. FoxHound9001

    FoxHound9001

    Joined:
    Sep 11, 2020
    Posts:
    8
    I tried to use the Set Active to destroy the game object and the victory message is true, but I can't do it. Someone help me?

    This is my code:

    obs: Read my code number 71

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PlayerContoller : MonoBehaviour , IInteractable
    7. {
    8.     float frente;
    9.     float re;
    10.     float girar;
    11.  
    12.     [Header("VIDA")]
    13.     public Image playerHealthFill;
    14.     public Image playerHealthFillRed;
    15.     public Image loseHd;
    16.     public float playerHealthMax = 100f;
    17.     public float playerHealthCurrent = 0f;
    18.  
    19.     [Header("BALA")]
    20.     public GameObject bullet;
    21.     public GameObject spawnBullet;
    22.  
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.         frente = 10;
    27.         re = 5;
    28.         girar = 60;
    29.  
    30.         HealthManager(playerHealthMax);
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void Update()
    35.     {
    36.         if (Input.GetKey(KeyCode.W))
    37.         {
    38.             transform.Translate(0, 0, (frente * Time.deltaTime));
    39.         }
    40.  
    41.         if (Input.GetKey(KeyCode.S))
    42.         {
    43.             transform.Translate(0, 0, (-re * Time.deltaTime));
    44.         }
    45.  
    46.         if (Input.GetKey(KeyCode.A))
    47.         {
    48.             transform.Rotate(0, (-girar * Time.deltaTime), 0);
    49.         }
    50.  
    51.         if (Input.GetKey(KeyCode.D))
    52.         {
    53.             transform.Rotate(0, (girar * Time.deltaTime), 0);
    54.         }
    55.  
    56.         if (Input.GetKeyDown(KeyCode.F))
    57.         {
    58.             Shooting();
    59.         }
    60.  
    61.     }
    62.  
    63.     private void OnCollisionEnter(Collision other)
    64.     {
    65.         if(other.gameObject.tag == "Enemy")
    66.         {
    67.             Debug.LogWarning("perdeu");
    68.         }
    69.     }
    70.  
    71.     void HealthManager(float value)
    72.     {
    73.         Debug.LogWarning("HealthManager");
    74.         Debug.LogWarning("value" + value);
    75.  
    76.         playerHealthCurrent += value;
    77.         playerHealthFill.fillAmount = playerHealthCurrent / 100;
    78.  
    79.         if(playerHealthFill.fillAmount == 0)
    80.         {
    81.             Destroy(this.gameObject);
    82.             playerHealthFillRed.gameObject.SetActive(false);
    83.             loseHd.gameObject.SetActive(true);
    84.         }
    85.  
    86.      
    87.  
    88.     }
    89.  
    90.     void Shooting()
    91.     {
    92.         Instantiate(bullet, spawnBullet.transform.position, spawnBullet.transform.rotation);
    93.     }
    94.  
    95.  
    96.  
    97.     public void Interact(float value)
    98.     {
    99.         Debug.LogWarning("HealthManager");
    100.         Debug.LogWarning("value" + value);
    101.  
    102.         playerHealthCurrent += value;
    103.         playerHealthFill.fillAmount = playerHealthCurrent / 100;
    104.  
    105.         HealthManager(-10);
    106.     }
    107. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,138
    My suggestion is to Debug.Log your values. What value are you getting for playerHealthCurrent and playerHealthFill.fillAmount and keep checking if you ever hit 0 when you expect to.
     
  3. FoxHound9001

    FoxHound9001

    Joined:
    Sep 11, 2020
    Posts:
    8
    It hits, but when using the loseHd.Set Active code, it doesn't work due to the Destroy Game Object

    Code (CSharp):
    1.  loseHd.gameObject.SetActive(true);
     
  4. FoxHound9001

    FoxHound9001

    Joined:
    Sep 11, 2020
    Posts:
    8
    My only trouble is this code:
    Code (CSharp):
    1. loseHd.gameObject.SetActive(true);
    2.  
    Code (CSharp):
    1. public Image loseHd;
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Nothing above is helpful to the troubleshooting process.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you want to be able to understand what is ACTUALLY happening in your code, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,138
    I would say, you could just switch the order of the destroy so it's the last call made in the if statement. Not sure if that's enough to get you what you want.