Search Unity

Multiplayer Inventory UI not fully updating.

Discussion in 'Multiplayer' started by LRodini, Oct 7, 2021.

  1. LRodini

    LRodini

    Joined:
    Aug 11, 2020
    Posts:
    6
    So, I have an Inventory script that has a list of items, and somehow, the code is working just fine, when 1 instance is running, but for some reason the Icon sprite will not show up when another client is added, nor will the inventory button work. In fact, it only works with the last client to join the game. The inspector valuer looks just fine for every instance, but in the game they are not working. Someone, please help.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class InventoryUI : MonoBehaviour
    6. {
    7.    
    8.     public GameObject invUI;
    9.     InventorySlot[] slots;
    10.     Inventory inv;
    11.     public PlayerControls player;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         inv = player.playerInventory;
    16.         inv.onItemChangedCallback += UpdateUI;
    17.         slots = GetComponentsInChildren<InventorySlot>();
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         if(player.controls.inv.GetControlBindingDown()){
    24.             invUI.SetActive(!invUI.activeSelf);
    25.         }
    26.     }
    27.     void UpdateUI(){
    28.         for(int i = 0; i<slots.Length; i++){
    29.             if (i<inv.itens.Count){
    30.                 slots[i].AddItem(inv.itens[i]);
    31.             }
    32.             else{
    33.                 slots[i].ClearSlot();
    34.             }
    35.         }
    36.     }
    37.    
    38. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class InventorySlot : MonoBehaviour
    7. {
    8.    
    9.     public Image icon;
    10.     [SerializeField]
    11.     Item item;
    12.     // Start is called before the first frame update
    13.  
    14.     public void AddItem (Item newItem){
    15.      
    16.         item = newItem;
    17.        
    18.         icon.sprite = item.icon;
    19.         Debug.Log("Icomadd");
    20.         icon.enabled = true;
    21.     }
    22.     public void ClearSlot(){
    23.        
    24.         item = null;
    25.         icon.sprite = null;
    26.         icon.enabled = false;
    27.     }
    28.     public void UseItem(){
    29.         if(item != null){
    30.             item.Use();
    31.             Debug.Log("Using");
    32.         }
    33.     }
    34. }