- Home /
Warning on creating an array of class objects
Hi, i have a problem with creating an array of class objects. I get the warning
UnityEngine.MonoBehaviour:.ctor() Weaponsscript:.ctor(Int32, Int32, String, Int32, Int32) Weaponsscript:Start() (at Assets/scripts/Weaponsscript.cs:43) lines(43 and near) are: public void Start () {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
weapons = new Weaponsscript[] { // 43 line new Weaponsscript(1, 1, "item1", 50, 500), new Weaponsscript(2, 0, "item2", 60, 750), new Weaponsscript(3, 0, "item3", 75, 1000), };
} Construcor: public Weaponsscript(int type, int lvl, string name, int dmg, int costgrowth) Array declaration Weaponsscript[] weapons; Thanks to all who answer.
Answer by gjf · Aug 09, 2014 at 06:26 PM
MonoBehaviour
's are added using AddComponent<>
. if you're class doesn't need to be a MonoBehaviour
, don't inherit from it...
I'm new at the Unity, so my code probably doesn't look good, but i need $$anonymous$$onoBehaviour in every script.
Answer by Graham-Dunnett · Aug 09, 2014 at 08:48 PM
Either:
a) Weaponsscript
is pure c# and doesn't use any Unity functions, in which case it can be treated like a regular c# class;
or
b) Weaponsscript
is a Unity script, in which case it'll have using UnityEngine;
at the top and will derive from MonoBehaviour
. These scripts do not use constructors, and instead get added to a GameObject in the inspector, or get added at run time using AddComponent()
. (This is what the error message says, btw.)
There's a pretty good documentation page on this here:
Okey, i'll read it again more detailed this time.
One more question: To introduce myself into the unity i started from making shop to be easier let's say a weapons shop. The constructor i used in the first post was to make x items in the shop. If you advice me to pin all of the objects seperately to empty gameobjects i think that would be a bit uncomfortable to use. When i wanted to add a GUI button to my shop i was making something like
for (int i = 0; i < 3; i++) {
if (GUI.Button (new Rect (i*130+30, 60, 100, 100), icons[weaponsscr.GetComponent<Weaponsscript>().getLvl(i)+(i*4)])) {
weaponsscr.GetComponent<Weaponsscript>().UpgradeItem (i);
}
}
But with seperated objects ins$$anonymous$$d of
Weaponsscript[] weapons;
Will make this thing harder.
I'll have to find another way how to do this.
Or you can advise me something again?
Answer by flaviusxvii · Aug 09, 2014 at 08:50 PM
You don't create instances of classes that inherit from MonoBehaviour directly. You make a new GameObject and then use AddComponent to attach your script.
Your answer
Follow this Question
Related Questions
What are GameTiles? 1 Answer
Custom Sort class array error if i use #pragma 2 Answers
Scope of variables: .NET arrays (UnityScript) 0 Answers
Not a member of Object..Can't get class.name from array 1 Answer
Help with sorting values from a class 2 Answers