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

UIImage.enabled = true dont work FIXED

Discussion in 'Scripting' started by HVutov, Apr 11, 2015.

  1. HVutov

    HVutov

    Joined:
    Mar 16, 2015
    Posts:
    18
    Hello everybody for some reason when i enable a image it dont enable the gameObject
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class SlotScript : MonoBehaviour {
    7.  
    8.     public Item item;
    9.     Image itemImage;
    10.     public int slotNumber;
    11.     InventoryManager inventory;
    12.  
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.         inventory = GameObject.FindGameObjectWithTag("Inventory").GetComponent<InventoryManager>();
    17.  
    18.         //firstChildOfObject
    19.         itemImage = gameObject.transform.GetChild(0).GetComponent<Image>();
    20.  
    21.  
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.  
    27.         //Shows items icon if there must be item in the slot
    28.         if (inventory.Items[slotNumber].itemName != null)
    29.         {
    30.             item = inventory.Items[slotNumber];
    31.             itemImage.enabled = true;
    32.             itemImage.sprite = inventory.Items[slotNumber].itemIcon;
    33.         }
    34.         else
    35.         {
    36.             itemImage.enabled = false;
    37.         }
    38.  
    39.     }
     
  2. HVutov

    HVutov

    Joined:
    Mar 16, 2015
    Posts:
    18
    The script adds properly the image of the slot but never enables the gameobject ingame i made a lot of debugging but did not found the problem
     
  3. Ostwind

    Ostwind

    Joined:
    Mar 22, 2011
    Posts:
    2,804
    Gameobject and component are not the same things. One gameobject can contain multiple components. If you want to also or only enable the gameobject:

    itemImage.gameObject.SetActive(true);
     
    HVutov likes this.
  4. HVutov

    HVutov

    Joined:
    Mar 16, 2015
    Posts:
    18
    Thanks man this was the solution i tried this but looks like i missed .gameObject. and nothing happend then
    You are awesome
     
  5. Ramael

    Ramael

    Joined:
    May 27, 2015
    Posts:
    2
    Hey man I had also the problem with showing/hiding an Image.
    I created a public Image but forgot to reference it in the Inspector :p so neither of these worked:

    itemImage.gameObject.SetActive(true/false)
    OR
    itemImage.enabled = true/false

    SOLUTION: After giving it a reference BOTH WORKED for showing and hiding!