So basically I created a class for items called Obtainable that contains things like item name, description, icon sprite etc.
Since it’s been a while since I’ve programmed anything in unity, I made the class derive from a MonoBehaviour. The script still runs and everything works fine but I get a warning message telling me I shouldn’t be creating new instances of a MonoBehaviour which makes sense. So I remove that part of the code but now I realise that I can’t use Obtainable.gameObject to destroy the object the obtainable is attached to.
So now I’m just wondering if there is a way to gain access to this variable in a script that is not a MonoBehaviour or if there is a better way of doing this that I’m not aware of.
here’s the class in case it helps:
[Serializable]
public class Obtainable
{
public Sprite Icon;
public string Name, Description, GetMsg, FailMsg;
public bool CanGet = true;
public static Obtainable Empty
{
get { return new Obtainable (null, "", "", canGet: false); }
}
public Obtainable (Sprite icon, string name, string description, string getMsg = "", string failMsg = "", bool canGet = true)
{
Icon = icon;
Name = name;
Description = description;
CanGet = canGet;
if (getMsg == "") GetMsg = "you got the " + Name + "!";
else GetMsg = getMsg;
if (failMsg == "") FailMsg = "you cannot get the " + Name + ".";
else FailMsg = failMsg;
}
private void Interact ()
{
ManagerScript.Instance.Inventory.GetItem (this);
}
}