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

[SOLVED] In script value is normal, but in UI value is doubled

Discussion in 'Scripting' started by zurisar, Feb 7, 2020.

  1. zurisar

    zurisar

    Joined:
    Nov 4, 2019
    Posts:
    24
    Hello guys, I have one more problem with my scripts. I don't serch and don't use google, because I don't know what I need to find. ;)
    I have some GUI for crafting items, I made popup like panel with information about item recipe, in this panel I show resources for recipe. Problem with items amount, one item show as it should, but another item show x2 amount, sometimes x3 amount, sometimes amount don't increase.

    Function that generate dynamic slots for resource items
    Code (CSharp):
    1. public void InitResourcePanel()
    2.     {
    3.         if(recipe)
    4.         {
    5.             foreach (RecipeContent recipeContent in recipe.Resources)
    6.             {
    7.                 GameObject instance = Instantiate(resourcesSlotPrefab);
    8.                 instance.transform.SetParent(resourcesSlotPanel);
    9.                 CraftPanelResourceSlotController slot = resourcesSlotPrefab.transform.GetComponent<CraftPanelResourceSlotController>();
    10.                 slot.item = recipeContent.item as Item;
    11.                 // Debug from here
    12.                 int needRec = recipeContent.amount;
    13.                 int playerWant = int.Parse(input.text);
    14.                 Debug.Log("Need: " + needRec + " want: " + playerWant + " total: " + needRec * playerWant);
    15.                 // To here
    16.                 int needValue = recipeContent.amount * int.Parse(input.text);
    17.  
    18.                 if (needValue > Inventory.instance.GetQty(item.itemID))
    19.                 {
    20.                     slot.valueColor = "red";
    21.                    
    22.                 }
    23.  
    24.                 slot._amnt = needValue;
    25.                 slot.SlotUpdate();
    26.             }
    27.         }
    28.     }
    Debug always show right numbers, but UI show
    FactoryUnleashed-error-gui-resourceamount.png

    See last console output, it's for paper glue.

    Function that put variables to prefab
    Code (CSharp):
    1. public void SlotUpdate()
    2.     {
    3.         //Debug.Log("Slot update: " + item.itemTitle + " " + _amnt.ToString() + "");
    4.         //Debug.Log("Check child " + transform.Find("Title"));
    5.         Image image = transform.Find("Image").GetComponent<Image>();
    6.         Text title = transform.Find("Title").GetComponent<Text>();
    7.         Text amount = transform.Find("Amount").GetComponent<Text>();
    8.  
    9.         if (item)
    10.         {
    11.             image.color = Color.white;
    12.             image.sprite = item.itemIcon;
    13.             title.text = item.itemTitle;
    14.             amount.text = "x " + _amnt.ToString();
    15.             if (valueColor == "red")
    16.             {
    17.                 amount.gameObject.GetComponent<Text>().color = Color.red;
    18.             }
    19.             else
    20.             {
    21.                 amount.gameObject.GetComponent<Text>().color = Color.white;
    22.             }
    23.         }
    24. }
    And finally function, which reload resources when variable change
    Code (CSharp):
    1.     public void ResetResourcePanel()
    2.     {
    3.         foreach (Transform child in resourcesSlotPanel)
    4.         {
    5.             GameObject.Destroy(child.gameObject);
    6.         }
    7.         StartCoroutine(WaitABit());
    8.     }
    9.  
    10.     public IEnumerator WaitABit()
    11.     {
    12.         yield return new WaitForSeconds(1);
    13.         InitResourcePanel();
    14.     }
    I thought that script can work too fast and add Coroutine with wat for 1 second, but this doesn't help.

    So, help me please find problem place in my code, because I haven't any more ideas. Thanks.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    If you uncomment line 3 in SlotUpdate, what does that log? Or, more generally: Debug.Log the value at every step of the process, and find where it starts logging the incorrect value.
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    On line 9, you are setting the variable "slot" to point to a component on the prefab instead of on the new instance you just created. So you make a copy for your current use, but then you modify the original instead of the copy.
     
  4. zurisar

    zurisar

    Joined:
    Nov 4, 2019
    Posts:
    24
    That old line, I checked does SlotUpdate get right values, just it. Anyway I sure that now UpdateSlot always get right values, as you can see, I uncomment that line
    FactoryUnleashed-error-gui-resourceamount2.png

    Script send right values from parent function InitResourcePanel and SlotUpdate get it right.
    I don't have idea what happend next
     
  5. zurisar

    zurisar

    Joined:
    Nov 4, 2019
    Posts:
    24
    Ohh, grate thanks!

    I change it to
    Code (CSharp):
    1. CraftPanelResourceSlotController slot = instance.transform.GetComponent<CraftPanelResourceSlotController>();
    And all work perfectly!