Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by marcin107 · Aug 09, 2014 at 06:14 PM · arrayclassobjects

Warning on creating an array of class objects

Hi, i have a problem with creating an array of class objects. I get the warning

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

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 () {
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.
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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...

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image marcin107 · Aug 09, 2014 at 08:40 PM 0
Share

I'm new at the Unity, so my code probably doesn't look good, but i need $$anonymous$$onoBehaviour in every script.

avatar image
0

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:

http://docs.unity3d.com/Manual/CreatingAndUsingScripts.html

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image marcin107 · Aug 10, 2014 at 09:11 AM 0
Share

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?

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges