- Home /
Instance of a class, adding it to a List
Hello !
I'm new to unity, but not that new to C# and I want to ask a really average and regular question which I don't know how to search after. I made a class called ConsumableItem.cs.
using UnityEngine;
using System.Collections;
public class ConsumableItem : MonoBehaviour {
public string _itemName;
public short _manaPlus;
public short _healthPlus;
public ConsumableItem (string itemName) {
_itemName = itemName;
if (_itemName == "Gyógyital") {
_manaPlus = 0;
_healthPlus = 40;
}
}
public short HealthPlus
{
get
{
return _healthPlus;
}
}
public short ManaPlus
{
get
{
return _manaPlus;
}
}
}
What I want to do is, I want to add an instance of the ConsumableItem to my Globals.cs class. My Globals.cs class looks like this:
using UnityEngine;
using System.Collections;
public class Globals : MonoBehaviour {
public static byte partyNumber = 2;
public static ArrayList Items = new ArrayList();
public static byte itemNumber = 5;
}
What I want to do would look like this in C#:
ConsumableItem a = new ConsumableItem ("Gyógyital");
Globals.Items.Add (a);
Of Course I got the error, which I've read about at several places
You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
So my question is, what's the proper method for doing anything like this in unity C#?
Thanks in advance !
Answer by robertbu · Nov 01, 2014 at 12:12 AM
You have two choices. The best choice given your information here is to not derive your ConsumableItem from MonoBehaviour. I don't see anything in the class that benefits from deriving it from MonoBehaviour, but this may not be the entire class. If you don't derive from MonoBehaviour, the it is just a standard C# class. You can use constructors and can create instances of the class using the 'new' operator.
Your second choice is to create your object as a component. You would do something like this:
GameObject go = new GameObject();
ConsumableItem a = go.AddComponent<ConsumableItem>();
a.SetItem("Gyógyital");
Globals.Items.Add(a);
Where 'SetItem()' is a new method that does what your constructor did in the original class.
Do I have to make it a gameObject ? What if I use it as a standard C# class? What I'd like to achieve is that in my Globals.cs there's an Item list, where I add and remove items throughout the game. When I enter the menu I want to make my items visible by looping through the Item list and getting it's name so I can later then display them in the menu and if I select it, I want to be able to get the _manaPlus and _healthPlus variables. So basically a standard inventory system.
Your answer
Follow this Question
Related Questions
Noob question about class inheritance 2 Answers
How to declare multiple variables using a for loop in class declaration? 0 Answers
Control which class is instantiated first 1 Answer
replacing instance of class from within itself 0 Answers
Saving class data without having lots of references? 1 Answer