Alright so I have a simple “store” UI that pops up when you hit a key. When you buy something by clicking one of the UI buttons, I want it to add that item to my list of items in my inventory script.
Here is the inventory script:
using UnityEngine;
using System.Collections;
public class Inventory : MonoBehaviour {
[SerializeField]
private invItem[] invItems = new invItem[10];
public void addInvItem(int index, invItem inItem)
{
for(int i = 0; i <= invItems.Length; i++)
{
if (i == index)
{
invItems[index] = inItem;
}
}
}
}
Here is the invItems that it is referencing:
public class invItem : MonoBehaviour {
public string itemName;
public int cost;
public invItem(string inName, int inCost){
itemName = inName;
cost = inCost;
}
}
and here is the button script:
[SerializeField]
private GameObject GameControl;
void Start()
{
}
public void addBasicTurret()
{
invItem basicTurret = new invItem("Basic Turret", 5);
GameControl.GetComponent<Inventory>().addInvItem(0, basicTurret);
}
I don’t get any errors, but when I try and use the button in order to add an invItem object into my array, I get this warning:
Any idea what I’m doing wrong here? I kinda understand that the warning is trying to tell me not to use “new” but I’m not sure how else to do it.