Search Unity

The way to check something once

Discussion in 'Scripting' started by Azux, Sep 15, 2018.

  1. Azux

    Azux

    Joined:
    Mar 3, 2015
    Posts:
    19
    Hi i'd like to ask how can i check a value once and depending of the result d something. It looks like that :
    Code (CSharp):
    1. if(value == true)
    2. float t += Time.deltatime;
    3.  
    4.  
    But i want to check the value just at begin of the script and later add time to t for rest of time:
    Code (CSharp):
    1. void Start(){
    2. if(value == true)
    3. AddTime();
    4. else
    5. DoubleTime();
    6. }
    7.  
    8. void AddTime(){
    9. float t += time.deltatime;
    10. }
    11.  
    12. void DoubleTime(){
    13. float t = 2* time.deltatime
    14. }
    So how can i start an "void Update" from Start or Awake?
    If there is a way then is that a better option insted of checking value every frame in Update if value is set before script starts?
     
  2. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    194
    Code (CSharp):
    1. float multiplier;
    2. float time;
    3. public bool myBool; //I guess you want this public to show in Inspector?
    4.  
    5. void Start()
    6. {
    7. multiplier = myBool == true ? 1f : 2f;
    8. }
    9. void Update()
    10. {
    11. time += Time.deltaTime * multiplier;
    12. }
     
  3. Azux

    Azux

    Joined:
    Mar 3, 2015
    Posts:
    19
    Ok i have lerned something thank You. And what if a bool is not a bool but enum? What i want to do is to check what type of enemy i have spawned so for example there is enum called warrior, archer,wizard and depending of the enemy class use different void. For now i have done it like that:
    Code (CSharp):
    1.   private void Update()
    2.     {
    3.         health_bar.transform.position = transform.position;
    4.  
    5.         if (ea.typeOfEnemy == Enemy_attributes.EnemyType.malee)
    6.         {
    7.             Malee();
    8.         }
    9.         else if (ea.typeOfEnemy == Enemy_attributes.EnemyType.ranged)
    10.         {
    11.             Ranged();
    12.         }
    13.     }
    But im not sure if checking the profession of enemy every frame make sens.
     
  4. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    194
    No, it doesn't make sense.
    You check it once (after you spawned the enemy for example), and based on that , you run your code accordingly.
    I also notice you are moving the health bar in Update, but your enemies could have an healthbar as child.
     
  5. Azux

    Azux

    Joined:
    Mar 3, 2015
    Posts:
    19
    So how can i check that typeOfEnemy just once?
    My health is a circle cuttoff and i dont know ho to do it as gameobject so i put his health into canvas where Image has option Radial.
     
  6. BrokawayGames

    BrokawayGames

    Joined:
    Feb 12, 2015
    Posts:
    4
    You can try something like this:
    Code (CSharp):
    1. public class YourClass: MonoBehaviour
    2. {
    3.     /* ... */
    4.  
    5.     System.Action _enemyHandler;
    6.     private void OnEnable()
    7.     {
    8.         _enemyHandler = (ea.typeOfEnemy == Enemy_attributes.EnemyType.malee) ? (System.Action)Malee : Ranged;
    9.     }
    10.  
    11.     private void Update()
    12.     {
    13.         health_bar.transform.position = transform.position;
    14.         _enemyHandler();
    15.     }
    16.  
    17.     private void Malee() { /* ... */ }
    18.     private void Ranged() { /* ... */ }
    19. }
     
  7. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    194
    Use a World Space Canvas.
     
  8. Azux

    Azux

    Joined:
    Mar 3, 2015
    Posts:
    19
    Oh for some reason i haven't try to put Canvas into an object in hierarchy. So my enemy now have a child which have canvas and image at the same object. Width and height are set at 0.25 and canvas is a square around my enemy and Health circle Is that what you had in mind?

    //Edit
    How can i add an picture here?