Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Ariel J · Aug 26, 2011 at 10:26 AM · c#editorarraynullreferenceexceptionlength

Need a hand using array.Length in a C# editor script

Hey everybody, I've been getting hounded by a nullreference error while using array.Length in a C# editor script

This is the editor script (With unnecessary script lines removed)

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
     [CustomEditor(typeof(SentryTurretAI))]
     public class SentryTurretAIEditor : Editor {
         int sections;
         bool[] showSentryPart;
         string[] aimFromObjects;
         
         public override void OnInspectorGUI () {
             EditorGUIUtility.LookLikeControls();
             SentryTurretAI sentryTurretAI = (SentryTurretAI) target as SentryTurretAI;
             sections = sentryTurretAI.sentryObjects.Length;
     }

And this is the actual script affected:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class trackObject {
         //These are variables in 'sentryObjects'
     public Transform rotatingObject;
     public float trackSpeed;
     public Vector3 up;
     public bool local;
     public bool x;
     public bool y;
     public bool z;
 }
 
     [System.Serializable]
     public class SentryTurretAI : MonoBehaviour {
         public trackObject[] sentryObjects;
         //Removed clutter
         }

The above posted is what I've narrowed down as the problem, I've been working at it for a while and have had no prevail! To the best of my knowledge, this should be working but it gives this error (ignore line reference):

NullReferenceException: Object reference not set to an instance of an object SentryTurretAIEditor.OnInspectorGUI () (at Assets/Editor/SentryTurretAIEditor.cs:14) UnityEditor.InspectorWindow.DrawEditors (Boolean isRepaintEvent, UnityEditor.Editor[] editors, Boolean eyeDropperDirty) UnityEditor.InspectorWindow.OnGUI () System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)

Any help would be absolutely amazing!

Thanks,

-AJ

Comment
Add comment · Show 1
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 Bunny83 · Aug 26, 2011 at 01:05 PM 0
Share

Well, can't see something wrong. $$anonymous$$aybe your target casting cause the problem since you double cast. $$anonymous$$eep in $$anonymous$$d when using the as-operator when the cast fails it returns null. Are you sure that your sentryTurretAI variable is not null ?

Also you should use only one cast. I do the cast usually in OnEnable and hold the reference in a public variable so i don't need to cast it every inspector update.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Marnix · Aug 26, 2011 at 10:37 AM

It seems that your sentryObjects is still null. Which it is, because you haven't defined sentryObject yet. Where is the line where you say: sentryObjects = new trackObject[5]?

Arrays need to be created, just like any other object. The problem with an array is that you have to specify the length at creation time.

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 Bunny83 · Aug 26, 2011 at 12:59 PM 1
Share

You're right, arrays needs to be created and also the contained objects needs to be created, but If you edit the property in the inspector the array AND the contained objects gets created by Unity. If you call sentryObjects = new trackObject[5] somewhere in your script you will delete all contained objects since you recreate the array.

avatar image Waz · Aug 26, 2011 at 01:11 PM 0
Share

Indeed, if they are in the inspector, even with no elements, you still get an array (with Length==0), not an undefined array.

avatar image Ariel J · Aug 26, 2011 at 01:43 PM 0
Share

Again, sorry for not posting about what I wanted as the output! I' just trying to get the length of the array, and the nullrefernceexception seems to be suggesting that the variable doesn't exist, rather than being equal to 0 or any other int. Thanks for replying though!

-AJ

avatar image Owen-Reynolds · Aug 26, 2011 at 02:52 PM 1
Share

You'd get compile-time errors if any variables didn't exist. Throw in if(sentryTurretAI.sentryObjects!=null) in front of everything. Or, to be extra cool, on null create a length 1 array (above,) and create and set-up sentry #0: trackObject TT = new trackObject(); sentryTurretAI.sentryObjects[0] = TT; TT.range=12; TT.type=1; ...

avatar image Ariel J · Aug 27, 2011 at 12:56 AM 0
Share

Wow! Thanks, using if(sentryTurretAI.sentryObjects!=null) did the trick!

-AJ

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

6 People are following this question.

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

Related Questions

Why ExecuteInEditMode Always Causes NullReferenceException Errors Even In Clamping!! 2 Answers

Manually creating Editor sometimes leads to NullReferenceException in SerializedObject constructor 0 Answers

How to display a list of methods and allow a choice of one of them 1 Answer

Distribute terrain in zones 3 Answers

NullReferenceException: Object reference not set to an instance of an object player.Update () (at Assets/player.cs:10) 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