Quantcast
Channel: Questions in topic: "class instance"
Viewing all articles
Browse latest Browse all 131

Instance of a class, adding it to a List

$
0
0
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 !

Viewing all articles
Browse latest Browse all 131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>