Search Unity

SImpel C# Question that i cant solve please help

Discussion in 'Scripting' started by Atles143, Feb 19, 2021.

  1. Atles143

    Atles143

    Joined:
    Dec 18, 2018
    Posts:
    10
    Hey Guys, did someone know how i can disable and able with a bool code?

    Here is my code and i want when dieAllowed false the script cant running the if(Hunger < 0.1)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AnimalLogic : MonoBehaviour
    6. {
    7.     public GameObject Animal;
    8.     public string AnimalName;
    9.  
    10.     public bool dieAllowed = true;
    11.     public bool hungerAllowed = true;
    12.     public bool healthRegeneration = true;
    13.  
    14.     public float Hunger = 100.0f;
    15.    
    16.    
    17.     void Start()
    18.     {
    19.        
    20.     }
    21.  
    22.  
    23.     void Update()
    24.     {
    25.         if(Hunger < 0.1)
    26.         {
    27.             Debug.Log(AnimalName + " dies on Hunger");
    28.             Destroy(Animal);
    29.         }
    30.     }
    31. }
    32.  
     
    Last edited: Feb 20, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    GameObjects are turned on and off with their
    .SetActive(bool)
    method.

    MonoBehaviors are turned of and on by setting their
    .enabled
    property.
     
  3. Atles143

    Atles143

    Joined:
    Dec 18, 2018
    Posts:
    10
    So i now make this, is this Ok too
    Code (CSharp):
    1. if(Hunger = true && Hunger < 0.1)
    2. {
    3. Destroy(Animal);
    4. }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Your post asked for "disable and able"... just beware that Destroy() is permanent.
     
  5. Atles143

    Atles143

    Joined:
    Dec 18, 2018
    Posts:
    10
    But what is by an permanent destroy false? After Destroying the GameObject the Script stop running