Search Unity

SetActive(True) One time on Update()

Discussion in 'Scripting' started by blackbrosg, Jan 15, 2020.

  1. blackbrosg

    blackbrosg

    Joined:
    Apr 9, 2018
    Posts:
    20
    hi guys, i need a this setactive(true) line run one time on update when my player lvl up, its possible? help me please!

    Code (CSharp):
    1. void Update() {
    2.        
    3.         if(player.level >= 2)
    4. {
    5.     levelup2.SetActive(true);
    6. }
     
  2. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    933

    Code (csharp):
    1.  
    2. private bool oneTime = false;
    3.  
    4. void Update {
    5.      if(player.level >= 2 && oneTime == false)
    6.        {
    7.          levelup2.SetActive(true);
    8.          oneTime = true;
    9.        }
    10. }
    11.  
     
  3. blackbrosg

    blackbrosg

    Joined:
    Apr 9, 2018
    Posts:
    20
    WORKS!! i love u Chocolade, thank u! <3
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I would suggest that you consider a different option. Instead of doing this in update, you do it when you check and set the player level to 2. While this might be a small bit of code, thus the impact is small, it's bad use. Because now you have this check running all the time. If you're trying to determine this when you change scenes, you can easily check for it in Start or Awake and turn it on at that point.

    Update is really meant for things that you need to repeatedly check for (like Input for example). Just don't get in the habit of using Update for everything because it seems simple.
     
    blackbrosg likes this.