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 khoatic · Apr 21, 2014 at 04:54 AM · editornullreferenceexceptioncustom-editor

Null Reference from Custom Editor

So Ive been getting null errors on the SoundObj In the DropAreaGUI function in the SoundInspector Class. Im unsure what im doing wrong, but even if i try and play it all the data gets wiped away and im really confused what Im doing wrong. please help.

//SoundManager

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 [System.Serializable]
 public class SoundManager : MonoBehaviour {
     
     [SerializeField]
     public List<SoundObj> list;

     public void Start()
     {
          list = new List<SoundObj>();
     }
 }


//SoundObj

 using UnityEngine;

 using System.Collections;
 using FMOD.Studio;
 
 [System.Serializable]
 public class SoundObj : MonoBehaviour
 {
    //Nothing in here right now
 }


//SoundInspector

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEditor;

 [CanEditMultipleObjects]
 [CustomEditor(typeof(SoundManager))]
 public class SoundInspector : Editor
 {

 private SerializedObject obj;
 public List<SoundObj> list;
 public SoundManager TargetObject;
 public List<bool> objFold;


 public void OnEnable()
 {
     obj = new SerializedObject(target);
     TargetObject = target as SoundManager;
     list = TargetObject.list;
     objFold = new List<bool>();
     if(list!=null){
         for (int n = 0; n < list.Count; n++)
             objFold.Add(true);
     }
 }
 private void RemovePoolAtIndex(int index)
 {
     for (int n = index; n < list.Count - 1; n++)
         list[n] = list[n + 1];

     list.RemoveAt(list.Count - 1);
 }
 public void DropAreaGUI()
 {
     Event evt = Event.current;
     Rect drop_area = GUILayoutUtility.GetRect(0.0f, 50.0f, GUILayout.ExpandWidth(true));
     GUI.Box(drop_area, "Add FMODASSET Into Box");

     switch (evt.type)
     {
         case EventType.DragUpdated:
         case EventType.DragPerform:
             if (!drop_area.Contains(evt.mousePosition))
                 return;

             DragAndDrop.visualMode = DragAndDropVisualMode.Copy;

             if (evt.type == EventType.DragPerform)
             {
                 DragAndDrop.AcceptDrag();

                 foreach (Object dragged_object in DragAndDrop.objectReferences)
                 {
                     FMODAsset asset = dragged_object as FMODAsset;
                     if (asset == null)
                     {
                         continue;
                     }
                     SoundObj obj = new SoundObj();
                     list.Add(new SoundObj());
                 }
             }
             Event.current.Use();
             break;
     }
 }

}

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
1
Best Answer

Answer by vexe · Apr 21, 2014 at 08:35 AM

Hello :) - A couple of things:

  1. UnityEngine.Objects are serialized by Unity by default so you don't need to mock your MonoBehaviours with System.Serializable - it's redundant.

  2. List < T > where T is a Serializable type, doesn't need to be mocked (decorated, tagged, etc) with SerializeField (Read this article to see what are those serializable types, and what conditions have to be met for them to serialize properly)

  3. You don't need to create a new SerializedObject for your target, you already have a serializedObject property that you get out of the box when you inherit Editor - so you could use that instead.

  4. The NullReferenceException issue you're having, is due to the fact that you can't new up/instantiate MonoBehaviours using the new operator - you can only use AddComponent to add them (See)

  5. Finally, you created a SerializedObject for your target, but you yet you're using a direct value to your list when removing items, which makes the whole point of using a SerializedObject pointless. I assume you intended to use SerializedProperties? - Then, to remove an element for your list via the serialized property that's pointing to it, you could just: spList.DeleteArrayElementAtIndex(index); (if your list is referencing UnityEngine.Objects, then the first call to DeleteArrayElementAtIndex will just null the element, call it again to remove it (stupid, I know...)

Comment
Add comment · Show 2 · 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 khoatic · Apr 22, 2014 at 04:17 AM 0
Share

in regards to the number 4 point you made, in looking at the link you supplied, it seems like im having to actually create objects in the scene in unity. Ideally what im looking to do is have the SoundObj objects just be stored in data and not in the scene. is there a way I can go about doing this?

avatar image vexe · Apr 22, 2014 at 07:56 AM 0
Share

If that's the case, you should doubt your need for them to be $$anonymous$$onoBehaviours - can't you just make them regular classes?

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

this error 10's of times in editor...? "NullReferenceException:..." 0 Answers

UnityEditor.InspectorWindow.DrawEditors null errors with no trace 0 Answers

Custom editor script for folders? 1 Answer

Can Runtime Classes be used in the Editor? 1 Answer

Unity Creating Invisible Clone (C#) 6 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