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

How to if(Setactive)?

Discussion in 'Scripting' started by unity_FUc_6LWWe7c3mw, May 8, 2021.

  1. unity_FUc_6LWWe7c3mw

    unity_FUc_6LWWe7c3mw

    Joined:
    Jan 1, 2021
    Posts:
    43
    Good day, i makea game for mobile.
    I want to Disable/Enable an Button when a integer is ..., but when i they dont check so my idea was to check if the LevelSelection Panel is active and then do thinks. But i dont no how to make a if statemant for SetActive.
    Thanks in Advance.
    Code (CSharp):
    1. void Update()
    2. {
    3. Level4Buttons
    4. }
    5.  
    6.     public void Level4Buttons()
    7.     {
    8.         if (GameObject.Find("Seite2").activeInHierarchy)
    9.         {
    10.             if (getNext.WhichLevel >= 4)
    11.             {
    12.                 GameObject.Find("Level4-ButtonIsNotAcitve").SetActive(true);
    13.                 GameObject.Find("Level4-Button").SetActive(false);
    14.                 Debug.Log("You can Play LV.4");
    15.             }
    16.             else
    17.             {
    18.                 {
    19.                     GameObject.Find("Level4-ButtonIsNotAcitve").SetActive(false);
    20.                     GameObject.Find("Level4-Button").SetActive(true);
    21.                     Debug.Log("You didn´t can play LV.4");
    22.                 }
    23.             }
    24.         }
    25.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    First, don't use GameObject.Find(). It is plagued with enough difficulties that if you google a bit you'll instantly understand.

    Second, your specific use above will never work for the simple reason that if you look in the documentation, you'll see the following line:

    "This function only returns active GameObjects."


    Make public fields for your GameObject and drag them in, or use some other kind of dynamic late-locator pattern if pre-authoring it does not work.

    But unless you really truly understand 100% of the costs and limitations of GameObject.Find() (again, review the documentation), do not use GameObject.Find().
     
    Bunny83 likes this.
  3. unity_FUc_6LWWe7c3mw

    unity_FUc_6LWWe7c3mw

    Joined:
    Jan 1, 2021
    Posts:
    43
    Ok, thanks but when i want to get my GamObject by a Script (Gamebject = Gameobject.Find), its dont work. i mean its because i had had it on a object that dont destroy on Load, did you know how to solve this problem?