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
  4. Dismiss Notice

Image.enable = true not working in Executed Function

Discussion in 'Editor & General Support' started by Spidermanisgay, Nov 4, 2021.

  1. Spidermanisgay

    Spidermanisgay

    Joined:
    Oct 17, 2019
    Posts:
    3
    I may just be a fool, however, any time I try to enable an image that is disabled in the inspector through this function, if for some reason fails to work. The value I'm passing into the function is correctly aligned with the image in my image array that I am trying to enable. I have tested this multiple times. The command to change the sprite of said image also functions correctly, so I know I have the right value passing into the function. My array is set up fine, I am not referencing these images in any other part of the script, or any other script for that matter. For the first set of images (I will label with an asterisk), I found a work around by making the images a child of blank game objects, and using SetActive(True) on the parent objects to achieve the same effect. However, the set I am trying to activate next is also not working(Marked by double asterisk). Something just seems amiss. I could use the same work around, but I truly do not understand why it isn't working. I've used the image.enabled on many different image arrays in the same damn script, but for some reason in this function it just fails to do so. For reference, the function is triggered by pressing 1 of 6 buttons, each of which passes a different value (0-5) into the "currentSpellSlot" int. Any advice? I can post the whole script if need be, but it's a bit of a doozy, it's also all working correctly otherwise. I'll also add the command I was using before I used the game objects as parents.

    *spellSlotImages[currentSpellSlot].enabled = true;

    public void fillSpellSlots(int currentSpellSlot)
    {

    if (craftManager.craftingHoldingID > 50 && spellSlotID[currentSpellSlot] < 0)
    {
    Debug.Log("" + currentSpellSlot);
    spellSlotID[currentSpellSlot] = craftManager.craftingHoldingID;
    spellSlotImages[currentSpellSlot].sprite = craftManager.craftingHoldingImage.sprite;
    *spellSlotContainer[currentSpellSlot].SetActive(true);
    magControl.spellSlotID[currentSpellSlot] = craftManager.craftingHoldingID - 51;
    craftManager.craftingHoldingID = 0;
    craftManager.craftingHoldingQuantity = 0;
    craftManager.holding = false;
    craftManager.craftingHoldingIcon.gameObject.SetActive(false);
    **magControl.spellImage[currentSpellSlot].enabled = true;
    magControl.spellImage[currentSpellSlot].sprite = craftManager.craftingHoldingImage.sprite;


    }
    }
     
  2. thecloudkeeper

    thecloudkeeper

    Joined:
    Sep 21, 2021
    Posts:
    28
    Weird. It's hard to tell without seeing more of the script/any other scripts that might be touching this. Would you be able to reproduce the issue in a standalone demo project and share the source?

    In the meantime, a possible hacky workaround would be to keep all Images enabled and call:
    spellSlotImages[currentSpellSlot].color = Color.white;

    to "enable" and
    spellSlotImages[currentSpellSlot].color = Color.clear;

    to "disable".
     
    Last edited: Nov 4, 2021
    Spidermanisgay likes this.
  3. Spidermanisgay

    Spidermanisgay

    Joined:
    Oct 17, 2019
    Posts:
    3
    I never considered using the color, this is actually a great and simple solution. Thanks a lot, I'll post the entire script after work.
     
    thecloudkeeper likes this.
  4. Spidermanisgay

    Spidermanisgay

    Joined:
    Oct 17, 2019
    Posts:
    3
    probably irrelevant at this point, but here's the full script. Honestly I'm sure it could be more concise, I'm a little new and I'm just brute forcing my way through what I want with what knowledge I have. No worries if you guys don't have the time to sift through it all. spellSlotImages isn't called anywhere beside the final function, so I really don't know how relevant the whole thing is.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class MagicCraftingPool : MonoBehaviour
    {
    public Image[] spellSlotImages;
    public Button[] spellSlotButtons;
    public Image[] magicPoolImages;
    public Button[] magicPoolSlots;
    public Sprite[] spellSprites;
    public Sprite[] runeSprites;
    public int[] magicPoolSlotsItemID;
    public int[] magicPoolSlotSpellID;
    public int[] spellSlotID;
    public int[] magicPoolButtonCorrelation;
    public int[] magicPoolSpellCorrelation;
    public GameObject craftingHoldingIcon;
    public CraftingManager craftManager;
    public InventoryManager invManager;
    public MagicController magControl;
    public int[] magicRecipes;
    public int[] magicOutputItemID;
    public GameObject[] spellSlotContainer;


    // Start is called before the first frame update
    void Start()
    {
    spellSlotButtons[0].onClick.AddListener(() => fillSpellSlots(0));
    spellSlotButtons[1].onClick.AddListener(() => fillSpellSlots(1));
    spellSlotButtons[2].onClick.AddListener(() => fillSpellSlots(2));
    spellSlotButtons[3].onClick.AddListener(() => fillSpellSlots(3));
    spellSlotButtons[4].onClick.AddListener(() => fillSpellSlots(4));
    spellSlotButtons[5].onClick.AddListener(() => fillSpellSlots(5));
    magicPoolSlots[0].onClick.AddListener(() => Exchange(0));
    magicPoolSlots[1].onClick.AddListener(() => Exchange(1));
    magicPoolSlots[2].onClick.AddListener(() => Exchange(2));
    magicPoolSlots[3].onClick.AddListener(() => Exchange(3));
    magicPoolSlots[4].onClick.AddListener(() => Exchange(4));
    magicPoolSlots[5].onClick.AddListener(() => Exchange(5));
    magicPoolSlots[6].onClick.AddListener(() => Exchange(6));
    /*for (int i = 0; i < magicPoolSlots.Length; i++)
    {
    magicPoolSlots.onClick.AddListener(() => Exchange(i));

    }*/
    craftManager = FindObjectOfType<CraftingManager>();
    invManager = FindObjectOfType<InventoryManager>();
    for (int i = 0; i < magicPoolImages.Length; i++)
    {
    magicPoolImages.enabled = false;
    }
    magControl = FindObjectOfType<MagicController>();
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void Exchange(int currentSlot)
    {
    //Debug.Log("" + currentSlot);
    if (craftManager.holding && magicPoolImages[currentSlot].enabled == false)
    {
    magicPoolImages[currentSlot].enabled = true;
    magicPoolSlotsItemID[currentSlot] = craftManager.craftingHoldingID;
    magicPoolButtonCorrelation[currentSlot] = craftManager.craftingHoldingButtonCorrelation;
    magicPoolImages[currentSlot].sprite = craftManager.craftingHoldingImage.sprite;
    craftManager.craftingHoldingQuantity -= 1;
    craftManager.craftingHoldingText.text = "x" + craftManager.craftingHoldingQuantity;
    if (craftManager.craftingHoldingQuantity <= 0)
    {
    Debug.Log("Bob");
    craftManager.craftingHoldingIcon.gameObject.SetActive(false);
    craftManager.holding = false;
    craftManager.craftingHoldingID = -1;
    craftManager.craftingHoldingButtonCorrelation = -1;
    }
    //craftManager.craftingHoldingID

    }
    else
    if (!craftManager.holding && magicPoolImages[currentSlot].enabled)
    {
    craftManager.craftingHoldingIcon.transform.position = Input.mousePosition;
    craftManager.craftingHoldingButtonCorrelation = magicPoolButtonCorrelation[currentSlot];
    magicPoolButtonCorrelation[currentSlot] = -1;
    craftManager.craftingHoldingIcon.gameObject.SetActive(true);
    craftManager.holding = true;
    craftManager.craftingHoldingImage.sprite = magicPoolImages[currentSlot].sprite;
    magicPoolImages[currentSlot].enabled = false;
    craftManager.craftingHoldingID = magicPoolSlotsItemID[currentSlot];
    magicPoolSlotsItemID[currentSlot] = -1;
    craftManager.craftingHoldingQuantity += 1;
    craftManager.craftingHoldingText.text = "x" + craftManager.craftingHoldingQuantity;

    }
    else
    if (craftManager.holding && magicPoolImages[currentSlot].enabled && craftManager.craftingHoldingID == magicPoolSlotsItemID[currentSlot])
    {
    magicPoolButtonCorrelation[currentSlot] = -1;
    magicPoolImages[currentSlot].enabled = false;
    craftManager.craftingHoldingID = magicPoolSlotsItemID[currentSlot];
    magicPoolSlotsItemID[currentSlot] = -1;
    craftManager.craftingHoldingQuantity += 1;
    craftManager.craftingHoldingText.text = "x" + craftManager.craftingHoldingQuantity;


    }
    else
    if (craftManager.holding && magicPoolImages[currentSlot].enabled && craftManager.craftingHoldingID != magicPoolSlotsItemID[currentSlot])
    {
    if (magicPoolSlotsItemID[currentSlot] < 51)
    {
    bool slotted = false;

    craftManager.craftingHoldingQuantity -= 1;
    craftManager.craftingHoldingText.text = "x" + craftManager.craftingHoldingQuantity;

    for (int i = 0; i < craftManager.lootListImage.Length; i++)
    {
    if (craftManager.lootListID == magicPoolSlotsItemID[currentSlot])
    {
    craftManager.lootListQuantity++;
    craftManager.lootListText.text = invManager.itemIDNames[magicPoolSlotsItemID[currentSlot]] + " x" + craftManager.lootListQuantity;
    i = craftManager.lootListImage.Length + 1;
    slotted = true;

    }
    }
    if (!slotted)
    {
    for (int i = 0; i < craftManager.lootListImage.Length; i++)
    {
    if (craftManager.lootListImage.enabled == false)
    {
    craftManager.lootListImage.enabled = true;
    craftManager.lootListText.enabled = true;
    craftManager.lootListID = magicPoolSlotsItemID[currentSlot];
    craftManager.lootListImage.sprite = magicPoolImages[currentSlot].sprite;
    craftManager.lootListQuantity = 1;
    craftManager.lootListText.text = invManager.itemIDNames[magicPoolSlotsItemID[currentSlot]] + " x" + craftManager.lootListQuantity;
    i = craftManager.lootListImage.Length + 1;

    }
    }
    }
    magicPoolImages[currentSlot].sprite = craftManager.craftingHoldingImage.sprite;
    magicPoolImages[currentSlot].enabled = true;
    magicPoolSlotsItemID[currentSlot] = craftManager.craftingHoldingID;
    craftManager.craftingHoldingButtonCorrelation = magicPoolButtonCorrelation[currentSlot];
    magicPoolButtonCorrelation[currentSlot] = -1;

    if (craftManager.craftingHoldingQuantity == 0)
    {
    craftManager.craftingHoldingID = 0;
    craftManager.craftingHoldingIcon.gameObject.SetActive(false);
    craftManager.holding = false;
    }
    } else
    {

    }
    }


    }
    public void Infuse()
    {
    string magicSlotIDString = magicPoolSlotsItemID[0].ToString() + magicPoolSlotsItemID[1].ToString() + magicPoolSlotsItemID[2].ToString() + magicPoolSlotsItemID[3].ToString() + magicPoolSlotsItemID[4].ToString() + magicPoolSlotsItemID[5].ToString() + magicPoolSlotsItemID[6].ToString();
    int magicSlotIDInt = int.Parse(magicSlotIDString);
    for(int i = 0; i < magicRecipes.Length; i++)
    {
    if (magicSlotIDInt == magicRecipes)
    {
    magicPoolImages[6].enabled = true;
    magicPoolImages[6].sprite = invManager.itemSprites[magicOutputItemID];
    magicPoolSlotsItemID[6] = magicOutputItemID;
    clearSlots();

    }
    }

    }
    public void clearSlots()
    {
    for(int i = 0; i < magicPoolSlots.Length - 1; i++)
    {
    magicPoolImages.enabled = false;
    magicPoolSlotsItemID = -1;
    }
    }
    public void fillSpellSlots(int currentSpellSlot)
    {

    if (craftManager.craftingHoldingID > 50 && spellSlotID[currentSpellSlot] < 0)
    {
    Debug.Log("" + currentSpellSlot);
    spellSlotID[currentSpellSlot] = craftManager.craftingHoldingID;
    spellSlotImages[currentSpellSlot].sprite = craftManager.craftingHoldingImage.sprite;
    spellSlotContainer[currentSpellSlot].SetActive(true);
    magControl.spellSlotID[currentSpellSlot] = craftManager.craftingHoldingID - 51;
    craftManager.craftingHoldingID = 0;
    craftManager.craftingHoldingQuantity = 0;
    craftManager.holding = false;
    craftManager.craftingHoldingIcon.gameObject.SetActive(false);
    magControl.spellImage[currentSpellSlot].enabled = true;
    magControl.spellImage[currentSpellSlot].sprite = craftManager.craftingHoldingImage.sprite;
    //magControl.spellImage[currentSpellSlot].color = Color.white;


    }
    }
    }
     
  5. thecloudkeeper

    thecloudkeeper

    Joined:
    Sep 21, 2021
    Posts:
    28
    Thanks for sharing! Looks like you're making a really cool magic system. While I can't spot any immediate problems, I will say that this script presents some design and formatting challenges that make it difficult for anyone to debug. I would recommend taking a look at a book like Robert Cecil Martin's Clean Code. It's a relatively quick read and I promise you that it will give you some incredible tools that will help to get to the bottom of this specific problem, as well as many more down the road.

    I geek out over taking a look at game mechanic systems, so (only if you want) feel free to share the coupled scripts to the above class (looks like
    CraftingManager
    ,
    InventoryManager
    , and
    MagicController
    ) and I'll do a deep-dive someday when I'm free. Either way, best of luck with this game!
     
  6. AltKreation

    AltKreation

    Joined:
    May 29, 2020
    Posts:
    1

    Thanks for posting this. Had to the same issue at OP, but this work around saved me.
     
    thecloudkeeper likes this.