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

Question Turning On/enable UI Image

Discussion in 'Scripting' started by axelsembayang456, Jun 20, 2023.

  1. axelsembayang456

    axelsembayang456

    Joined:
    Jun 20, 2023
    Posts:
    1
    Hi, I have this canva's GameObject which has 3 childs(Key, Key2, BG) as UI Image. I set the Key and Key2 images to be off/disabled and i want it to turn it on/enable in a condition (inside another object script). I used every way that i know but it won't turn on.
    upload_2023-6-21_0-17-30.png
    upload_2023-6-21_0-17-15.png
    upload_2023-6-21_0-16-51.png
    upload_2023-6-21_0-16-35.png
     
  2. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    i think you need to do gameobject.setactive(true);

    alternatively you can change color.a=0
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    First decide if you are:

    - using SetActive( true / false) on the GameObject

    - using .enabled = true / false on the Button itself

    - using the .interactable property on the Button itself.

    Regardless of what you choose, the process for manipulating objects in Unity is always the same:

    Referencing variables, fields, methods (anything non-static) in other script instances:

    https://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639

    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    It isn't always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

    Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

    That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.