Search Unity

Image Active/Inactive issue

Discussion in 'Scripting' started by Ztorum, Jan 15, 2019.

  1. Ztorum

    Ztorum

    Joined:
    Sep 7, 2018
    Posts:
    4
    Hello Guys,

    I have a script in a gameobject (my player). In the script, I am trying to edit make an image active/inactive, which is a child canvas. Might be easy issue but I already spent 1 day on this without a solution :/

    I checked the net and could not find an answer about this issue. Could someone help me please? Btw, the player is already active in the scene, I have read several topics about calling a function inside an inactive gameobject :)

    Additionally, I checked that the image findingscript correctly references the related image.

    Output of the Script:
    Initial Status: False
    Status After Inactive: False
    Status After Active: False


    Why this code is not working?

    public class Spell : MonoBehaviourPun
    {
    Image fillLowManaImage_1;
    }

    void Awake()
    {
    fillLowManaImage_1 = FindImageObject("Spell1_LowMana").GetComponent<Image>();

    //the image is already active as default.
    Debug.Log("Initial Status: " + fillLowManaImage_1.IsActive());
    fillLowManaImage_1.gameObject.SetActive(false);
    Debug.Log("Status After Inactive: " + fillLowManaImage_1.IsActive());
    fillLowManaImage_1.gameObject.SetActive(true);
    Debug.Log("Status After Active: " + fillLowManaImage_1.IsActive());

    }


    private Image FindImageObject(string ImageName)
    {
    Image[] firstList = Resources.FindObjectsOfTypeAll<Image>();
    List<Object> finalList = new List<Object>();
    int value = 0;

    for (int i = 0; i < firstList.Length; i++)
    {
    if (firstList.gameObject.name == ImageName)
    {
    value = i;
    }
    }

    return firstList[value];
    }
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    I'm not sure, but maybe SetActive does not take effect until the end of the frame.
     
  3. Ztorum

    Ztorum

    Joined:
    Sep 7, 2018
    Posts:
    4
    I also moved the script to another object instead of instantiated player object. Still same issue :/

    Additionally, instead of findobject, if I use a public image variable and assign the image in the inspector, it works fine!!! But still dont know how to correct it via script...
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    Have you tried it in Start instead of Awake?
     
  5. Ztorum

    Ztorum

    Joined:
    Sep 7, 2018
    Posts:
    4
    I tried, it did not work.

    Btw, I finally solved the problem. In my image search function, I used Resources.FindObjectsOfTypeAll<Image>() intentionally, because FindObjectsOfType<Image>() does not find inactive gameobjects which is the case for my game.

    However, this is the reason of the problem, when I use Resources.FindObjectsOfTypeAll<Image>(), the SetActive command does not work!!! I dont know why... But anyway, I changed to FindObjectsOfType<Image>() again and problem solved... I manually diable the image inside the script after variable assignment.

    Thanks for your help and response.