Search Unity

Resolved How to disable/enable Image element in a UI Canvas

Discussion in 'UGUI & TextMesh Pro' started by GladisAd, Jun 7, 2023.

  1. GladisAd

    GladisAd

    Joined:
    Oct 5, 2014
    Posts:
    28
    Hi,

    I've been trying to find the GetComponent<> function that would allow me to disable the 'Image' element in the picture below, but am at a total loss. I've tried adding UIElements, TMPro, to the code to access the UI but can't seem to find anything that works. Any help would be much appreciated.

    https://www.dropbox.com/s/0vt1umazmdh1xuq/Image.jpg?dl=0

    Many thanks.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    That's not a great strategy.

    UI Canvases are part of the UnityEngine.UI namespace.

    UIToolkit is a completely unrelated and incomplete (in my opinion) package.

    TMPro is a text package that includes some Components capable of interoperating and/or dependent upon UnityEngine.UI namespaces.

    I suggest that you re-pose your question after looking at the relevant documentation to what you are doing.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Remember the first rule of GameObject.Find():

    Do not use GameObject.Find();

    More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

    More information: https://forum.unity.com/threads/why-cant-i-find-the-other-objects.1360192/#post-8581066

    In general, try NOT to use "find" or "get" style methods unless there truly is no other way. Drag references in for the highest rate of Unity success.

    UnityEngine.UI is now a package:

    https://forum.unity.com/threads/where-did-the-ui-option-in-the-create-menu-go.1134796/#post-7290742
     
  3. GladisAd

    GladisAd

    Joined:
    Oct 5, 2014
    Posts:
    28

    - what you want
    I've been trying to find the GetComponent<> function that would allow me to disable the 'Image' element in the UI.

    - what you tried
    Adding the following to establish the code required to GetComponent<>().:
    using UnityEngine.UIElements;
    using UnityEngine.UI;
    using TMPro;

    - what you expected to happen
    For a relevant GetComponent<> option to appear that would allow me to disable/enable the 'Image' element.

    - what actually happened, log output, variable values, and especially any errors you see
    Nothing, because i couldn't find any suitable code that would allow me to test the code.

    - links to documentation you used to cross-check your work (CRITICAL!!!)
    I haven't found any, hence why I'm posting on the forums. I've searched the internet but all I can find are things relating to hiding the canvas, not the elements within. I've tried the Unity Scripting API page also.

    I don't have any code snippets because I haven't been able to produce working code.

    Outside of that, I'm not sure what you're asking of me?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    The steps to success will be:

    - install the UnityEngine.UI package (sounds like you already have if you have an Image)

    - make a
    public Image MyImage;
    variable in your script

    - drag the image reference in the My Image slot

    - access it with
    MyImage.enabled = false;
     
  5. GladisAd

    GladisAd

    Joined:
    Oct 5, 2014
    Posts:
    28
    That's great. Worked perfectly. Many thanks.
    It's clear my understanding of UI components is lacking.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736