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 /
This question was closed Aug 10, 2014 at 09:34 PM by JSierraAKAMC for the following reason:

Answered by robertbu

avatar image
0
Question by JSierraAKAMC · Aug 10, 2014 at 07:23 PM · c#listnullreferenceexceptiononinspectorgui

Why does this return a NullReferenceException?

I am creating a procedural item placement script and editor. The editor allows for adding custom "SceneryObjects" and setting their properties in the inspector. There is also an option to delete the SceneryObject with an "X" button. However, upon clicking the "X" button on an SceneryObject without a GameObject assigned to it, I get a NullReferenceException. After assigning any GameObject to the SceneryObject and clicking the "X" button again, the SceneryObject is deleted. Why is the "X" button not working for SceneryObjects without GameObjects set to them?

SceneryObject.cs :

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class SceneryObject{
     public string name;
     [Range(0.0f, 1.0f)]
     public float spawnProbability;
     public float minHeight;
     public float maxHeight;
     [Range(0.0f, 90.0f)]
     public float minSteepness;
     [Range(0.0f, 90.0f)]
     public float maxSteepness;
     public bool randRotX = true;
     public bool randRotY = true;
     public bool randRotZ = true;
     public GameObject obj;
     public bool makeStatic = true;
     public Vector3 minScale;
     public Vector3 maxScale;
 }
 

TestItemPlacement.cs :

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [ExecuteInEditMode]
 public class NewItemPlacement : MonoBehaviour {
 
     public Terrain terr;
     public Transform cloneParent;
     public List<SceneryObject> objs = new List<SceneryObject>();
     public void checkAndPlace(Terrain terr, SceneryObject sO, int aMPX, int aMPY, Vector2 aMPos, Transform par)
     {
         //Generation code
     }
 
     public void ScanAndPlace(Terrain terr, SceneryObject so)
     {
         //More code
     }
 
     public void ClearAll(string nam, Transform par)
     {
         //And more code
     }
 
 }
 

TestPlacementEditor.cs :

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEditor;
 
 [CustomEditor(typeof(NewItemPlacement))]
 public class ItemPlacementEditor : Editor {
 
 
     NewItemPlacement myScript;
 
     void OnEnable()
     {
         myScript = (NewItemPlacement)target;
     }
 
     public override void OnInspectorGUI()
     {
         DrawDefaultInspector();
         if(GUILayout.Button ("Add"))
         {
             myScript.objs.Add(new SceneryObject());
         }
         for(int i = 0; i < myScript.objs.Count; i ++)
         {
             GUILayout.BeginHorizontal ();
             if(GUILayout.Button ("Generate " + i))
             {
                 myScript.ScanAndPlace(myScript.terr, myScript.objs[i]);
             }
             if(GUILayout.Button ("Delete " + i))
             {
                 myScript.ClearAll (myScript.objs[i].obj.name, myScript.cloneParent);
             }
             if(GUILayout.Button ("X", GUILayout.Width (20)))
             {
                 myScript.ClearAll(myScript.objs[i].obj.name, myScript.cloneParent);
                 myScript.objs.RemoveAt (i);
             }
             GUILayout.EndHorizontal();
         }
 
     }
 }
 

Comment
Add comment · Show 3
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 robertbu · Aug 10, 2014 at 09:00 PM 1
Share

Please post a copy of the error message from the console. Note I don't think TestPlacementEditor.cs will compile. 'target' is not declared anywhere.

avatar image JSierraAKAMC · Aug 10, 2014 at 09:33 PM 0
Share

Figured it out because of you! Thanks! I should have read the entire error message! Thanks!

avatar image Supremacy037 · Aug 10, 2014 at 09:42 PM 0
Share

You're calling obj.name in your ClearAll method, so it's throwing a null reference if obj (Your GameObject) isn't set.

0 Replies

  • Sort: 

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

A node in a childnode? 1 Answer

for loop in OnInpectorGUI giving NullReferenceException error for some reason? 1 Answer

Attached scripts as List? 1 Answer

Custom editor desperation: instance of custom class 1 Answer

C# NullReferenceException 1 Answer


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