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 mirek.um · Oct 28, 2014 at 02:02 PM · listclassmonobehaviour

List of Monobehaviour clasess, Editor

I have 2 scripts. SpawnerM2 and SpawnerM2Editor.

First script contains 2 clases:

 [System.Serializable]
 public class SpawnerM2Single : MonoBehaviour
 {
     // some variables
 
     void Update () 
     {
         // some code
     }
 }
  
 [System.Serializable]
 public class SpawnerM2 : MonoBehaviour 
 {
     public List<SpawnerM2Single> single = new List<SpawnerM2Single>();
 }


second script:

 [CustomEditor(typeof(SpawnerM2))]
 public class SpawnerM2Editor : Editor 
 {
     private SerializedObject spawnerM2SO;
     private SpawnerM2 mySpawnerM2;
     
     void OnEnable () 
     {
         spawnerM2SO = new SerializedObject(target);
         mySpawnerM2 = (SpawnerM2)target;
     }
     
     public override void OnInspectorGUI () 
     {
         spawnerM2SO.Update();
         
         EditorGUILayout.BeginVertical();
         
             int i = 0;
             
             foreach(SpawnerM2Single sp in mySpawnerM2.single)
             {
                 EditorGUILayout.Foldout(true,"Spawner #" + i);
                 {  
                     // some code
                 }
                 i++;
 
             }
         
         EditorGUILayout.EndVertical();
         
         if (GUILayout.Button("Add spawner")) 
         {
             // ERROR HERE
             mySpawnerM2.single.Add(new SpawnerM2Single());
         }
         
         if (GUILayout.Button("Remove last spawner") && mySpawnerM2.single.Count > 0) 
         {
             mySpawnerM2.single.RemoveAt(mySpawnerM2.single.Count-1); 
         }
         
         if(GUI.changed)
         {
             EditorUtility.SetDirty(target);
             EditorUtility.SetDirty(mySpawnerM2);
         }
         
         spawnerM2SO.ApplyModifiedProperties();
     }    
 }


I get an error message:

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() SpawnerM2Single:.ctor() SpawnerM2Editor:OnInspectorGUI() (at Assets/Editor/SpawnerM2Editor.cs:57) UnityEditor.DockArea:OnGUI()

How Can I change the code to make it works

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by _dns_ · Oct 28, 2014 at 02:26 PM

Hi, to create a MonoBehavior based class you should not use "new SpawnerM2Single()", Unity needs to create the object itself.

You can create a new gameObject and then add Components to it like described here: http://docs.unity3d.com/ScriptReference/GameObject-ctor.html. You can also Instantiate an existing prefab, in the Editor using: http://docs.unity3d.com/ScriptReference/PrefabUtility.InstantiatePrefab.html

Also, btw, you don't need to add [System.Serializable] to a Monobehavior-based class.

Comment
Add comment · Show 5 · 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 mirek.um · Oct 28, 2014 at 07:27 PM 0
Share

I changed it to :

 mySpawner$$anonymous$$2Single = (new GameObject("SingleSpawner")).AddComponent<Spawner$$anonymous$$2Single>();

It works, but it creates SingleSpawner GameObject, on my scene, everytime I click component in the inspector

How can I avoid it?

avatar image _dns_ · Oct 28, 2014 at 08:51 PM 0
Share

Well, it depends on what you want to do. In Unity, a gameobject is either in the form of a prefab in a file asset, or as an instance in a scene (well, prefabs referenced in a scene are also instantiated in memory by Unity and act like a kind of memory-mapped object/file in the Editor).

There are also ScriptableObjects that share a lot with gameobjects but are not "active" gameobjects and then are great to store data as they don't consume CPU and can be stored in assets.

What exactly do you want to do ?

avatar image mirek.um · Oct 28, 2014 at 09:08 PM 0
Share

I don't need the new GameObject. I just use it to "instantiate" $$anonymous$$onobehaviour Spawner$$anonymous$$2Single class.

What I exactly need is component like this on the picture alt text

component.png (16.2 kB)
avatar image _dns_ · Oct 28, 2014 at 11:16 PM 0
Share

Usually, what I do for this kind of situation is to have 1 gameobject prefab for each spawner type. Just create an instance of 1 spawner in the scene, then drag&drop it in your Project/Asset to create a first prefab. Then, you can edit this prefab as you want. You can then select it and use "ctrl+D" to duplicate the file and create another spawner type that you can rename and edit as you want, and so on.

To have a list of those prefabs, you can have a gameobject+component that will contain a list of those gameobjects+component, just like you have with Spawner$$anonymous$$2. With the default inspector, you'll be able to add & remove elements and drag & drop the previously created spawner prefabs in the list's elements to reference them (try right click menu on the list's elements for additional options). You can also create a prefab of the spawnerlist Spawner$$anonymous$$2 and drag & drop prefabs into the List of the prefab (not an instance in scene, just reference prefab from a prefab). This is interesting because you can easily duplicate this list and only change a few elements. Doing this way, if you modify a spawner's values, it will then be changed in all the lists it's referenced from.

Last step is to reference the list's prefab from a public field of a "game manager" object in your scene so you can access it. Warning, any change made to the prefabs in memory will be written to disk during next save. It can be interesting but also dangerous. Instantiate the list from the prefab if you want an independent object/instance.

You can also use a custom inspector to have the kind of view you want, expect to spend some time developing this custom inspector though, that's not so simple. I think you have all the functionalities you want with the default prefab system. It will be faster to use it now and maybe develop a custom inspector later.

(you could also use ScriptableObjects but that's more complicated to implement)

avatar image mirek.um · Oct 30, 2014 at 07:11 PM 0
Share

Thank you. I found a nice piece of code that solves my problem http://forum.unity3d.com/threads/display-a-list-class-with-a-custom-editor-script.227847/

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

how do I remove a class item from a list? 1 Answer

Accesing custom class object from another script 1 Answer

Level generation using a "tree" type structure, why when i add a cell i get an error argument out of range? 1 Answer

I'm Trying to Access a Custom Class and can't quite get the overload technique. 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