Hi I am begginning to develop game in Unity3d and I have bug in my code or in inspector.
My bug is in the array arthursButtonsDescription where I am assigning itemToEquip twice. First turn everything goes well arthursButtonsDescription[0].name is "0", but second turn i assign arthursButtonsDescription[4].name=4 and arthursButtonsDescription[0].name is 4 too and here is my bug.
I recorded video about this bug: https://youtu.be/wZuFo5uhL5o
And I uploaded my bugged project to: https://uloz.to/file/zfyxYuOiHTa4/dandd-zip#!ZGp4LmR2AzV0MTSvMGxkMwOvZwEuBGEuIRuhIyM6MJAgFGIuAj==/dandd-zip
using UnityEngine;
public class Item : MonoBehaviour
{
public Sprite icon = null;
public bool showInInventory = true;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Equipment : Item
{
public EquipmentSlot equipSlot;
new public string name = "New Item";
}
public enum EquipmentSlot { Head, Chest, Legs, Foot, Shield, Weapon,
Potion20Hp, Potion40Hp, Potion60Hp, Potion80Hp, Potion100Hp,
Potion25Percent, Potion50Percent, Potion75Percent, Potion100Percent }
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveLoadStarter : MonoBehaviour
{
public void LoadClick()
{
Equipment itemToEquip = gameObject.AddComponent<Equipment>();
itemToEquip.name = "0";
itemToEquip.equipSlot = EquipmentSlot.Head;
Debug.Log("iTE0: " itemToEquip.name);
EquipmentManager.instance.Equip(itemToEquip);
itemToEquip.name = "4";
itemToEquip.equipSlot = EquipmentSlot.Shield;
Debug.Log("iTE4: " itemToEquip.name);
EquipmentManager.instance.Equip(itemToEquip);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EquipmentManager : MonoBehaviour
{
public static EquipmentManager instance;
public void Awake()
{
if (instance != null)
{
Debug.Log("EquipmentManager instance not equals null");
return;
}
instance = this;
}
public ArthurInventory ai;
public void Equip(Equipment newItem)
{
ai = FindObjectOfType(typeof(ArthurInventory)) as ArthurInventory;
ai.ShowArthursButtons(newItem);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ArthurInventory : MonoBehaviour
{
public GameObject[] arthursButtons/* = new GameObject[6]*/;
public Equipment[] arthursButtonsDescription = new Equipment[6];
public void ShowArthursButtons(Equipment newItem)
{
for (int i = 0; i < arthursButtons.Length; i )
{
if (arthursButtons[i].name == "HellmButton" && newItem.equipSlot == EquipmentSlot.Head)
{
arthursButtons[i].GetComponentInChildren<Button>().GetComponentInChildren<Image>().enabled = true;
arthursButtonsDescription[0] = newItem;
}
if (arthursButtons[i].name == "ShieldButton" && newItem.equipSlot == EquipmentSlot.Shield)
{
arthursButtons[i].GetComponentInChildren<Button>().GetComponentInChildren<Image>().enabled = true;
arthursButtonsDescription[4] = newItem;
Debug.Log("AI0: " arthursButtonsDescription[0].name);
}
}
}
}
CodePudding user response:
Looks like your problem is in SaveLoadStarter.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveLoadStarter : MonoBehaviour
{
public void LoadClick()
{
Equipment itemToEquip = gameObject.AddComponent<Equipment>();
itemToEquip.name = "0";
itemToEquip.equipSlot = EquipmentSlot.Head;
Debug.Log("iTE0: " itemToEquip.name);
EquipmentManager.instance.Equip(itemToEquip);
itemToEquip.name = "4";
itemToEquip.equipSlot = EquipmentSlot.Shield;
Debug.Log("iTE4: " itemToEquip.name);
EquipmentManager.instance.Equip(itemToEquip);
}
}
You declare itemToEquip, set it, then set it again.
You need to create a new Equipment object. Otherwise you are just changing the values in the first object.
Try something like this:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveLoadStarter : MonoBehaviour
{
public void LoadClick()
{
Equipment itemToEquip = gameObject.AddComponent<Equipment>();
itemToEquip.name = "0";
itemToEquip.equipSlot = EquipmentSlot.Head;
Debug.Log("iTE0: " itemToEquip.name);
EquipmentManager.instance.Equip(itemToEquip);
itemToEquip = gameObject.AddComponent<Equipment>();
itemToEquip.name = "4";
itemToEquip.equipSlot = EquipmentSlot.Shield;
Debug.Log("iTE4: " itemToEquip.name);
EquipmentManager.instance.Equip(itemToEquip);
}
}