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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Hide image control

Discussion in 'Scripting' started by game4tress, May 21, 2015.

  1. game4tress

    game4tress

    Joined:
    May 13, 2010
    Posts:
    46
    I'm trying to hide an image control through code, but it's not working.
    I've tried the following code:
    ImageT.GetComponent<Renderer>().enabled = false;

    but I get a runtime error saying:
    There is no 'Renderer' attached to the "ImageT" game object, but a script is trying to access it.
     
  2. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    If you are using the new UI, I think you need to get the Canvas not Renderer and disabled that. Failing that set the images alpha property to full transparent - 1.0
     
  3. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    It's telling you the gameObject this script is on or whatever imageT is doesn't have a Render component...Does the object have a mesh render?
     
  4. game4tress

    game4tress

    Joined:
    May 13, 2010
    Posts:
    46
    Thank you all for your answers.
    No, SubZeroGaming, it doesn't.
     
  5. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    That's the issue. You can't access a property of a component if it doesn't exist. If you are working with the new UI, grab the Image component and then set enable to false. Not the Render.
     
  6. game4tress

    game4tress

    Joined:
    May 13, 2010
    Posts:
    46
    Yes, I'm working with the new UI.
    How do you mean "grab the Image component and then set enable to false"?
    Like this?
    ImageT.GetComponent().enabled=false;
    or like this?
    ImageT.enabled=false;

    Both dont work.
    This is my code:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Script_TestResizeAgain : MonoBehaviour {
    6.     private GameObject PlaneT;
    7.     private GameObject ImageT;
    8.  
    9.  
    10.     void Start () {
    11.  
    12.         PlaneT=GameObject.Find("PlaneT");
    13.         ImageT=GameObject.Find("ImageT");
    14.      
    15.         PlaneT.transform.position=ImageT.transform.position;
    16.         PlaneT.transform.localScale=ImageT.transform.localScale;
    17.  
    18.         ImageT.GetComponent<Renderer>().enabled = false;
    19.     }  
    20. }
    21.  
    22.  
     
    Last edited: May 21, 2015
  7. game4tress

    game4tress

    Joined:
    May 13, 2010
    Posts:
    46
    I get the same error with the canvas
    MissingComponentException: There is no 'Renderer' attached to the "CanvasT" game object, but a script is trying to access it. You probably need to add a Renderer to the game object "CanvasT". Or your script needs to check if the component is attached before using it

    Here is the code
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5.  
    6.  
    7. public class Script_TestResizeAgain : MonoBehaviour {
    8.  
    9.     private GameObject PlaneT;
    10.     private GameObject ImageT;
    11.     public GameObject CanvasT;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.        
    16.         PlaneT=GameObject.Find("PlaneT");
    17.         ImageT=GameObject.Find("ImageT");
    18.         CanvasT=GameObject.Find("CanvasT");
    19.        
    20.         PlaneT.transform.position=ImageT.transform.position;
    21.         PlaneT.transform.localScale=ImageT.transform.localScale;
    22.  
    23.         CanvasT.GetComponent<Renderer>().enabled=false;
    24.     }
    25.    
    26.     // Update is called once per frame
    27.     void Update () {
    28.        
    29.     }
    30. }
    31.  
     
  8. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    No I meant you need to get the canvas component GetComponent<Canvas>
     
  9. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    No, you don't need the canvas component to turn off an Image...you simply grab the Image...

    You need the using unity engine.ui namespace.
    Then you need a handle to your Image component.

    public Image myImage; //assign it in the inspector.

    Then you can do myImage.enabled = false.

    or in void start()

    Assign the handle. myImage = get component<image>


    I'm not going to spoon feed you. Watch my tutorials and learn something.
     
  10. game4tress

    game4tress

    Joined:
    May 13, 2010
    Posts:
    46
    Thank you all.
    It works, like Korno said.