• 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, 2017 at 07:50 AM by $$anonymous$$ for the following reason:

The question is answered, right answer was accepted

avatar image
Question by $$anonymous$$ · Aug 10, 2017 at 07:14 AM · arraynullreferenceexceptionarraysclasslength

Why Again Arrays In ExecuteInEditMode() Gives NullReferenceException Errors??

Now I'm Facing Another Problem With Array.Length In ExecuteInEditMode().
I'm Trying To Make An Automatic Button To Set And Create Default Recommended Values, So When I Try To Change A Value From The Array(Class), It Gives Me NullReferenceException!
Here Is An Example Script I Made Which Doesn't Work: -C#:

 using UnityEngine;
 [ExecuteInEditMode()]
 public class TEST : MonoBehaviour
 {
     public bool create = false;
     public MyClass[] myType = new MyClass[0];
     [System.Serializable]
     public class MyClass
     {
         public float myVar = 0;
         public int _myVar = 0;
         public Transform m_myVar = null;
     }
     public void Update ()
     {
         if(create)
         {
             if(myType.Length == 0)myType = new MyClass[1];
             if(myType[0].myVar == 0)myType[0].myVar = 1;
             if(myType[0]._myVar == 0)myType[0]._myVar = 2;
             if(myType[0].m_myVar == null)myType[0].m_myVar = transform;
             create = false;
         }
     }
 }

-JavaScript:

 @script ExecuteInEditMode()
 public var create : boolean = false;
 public var myType : MyClass[] = new MyClass[0];
 public class MyClass
 {
     public var myVar : float = 0;
     public var _myVar : int = 0;
     public var m_myVar : Transform = null;
 }
 public function Update ()
 {
     if(create)
     {
         if(myType.Length == 0)myType = new MyClass[1];
         if(myType[0].myVar == 0)myType[0].myVar = 1;
         if(myType[0]._myVar == 0)myType[0]._myVar = 2;
         if(myType[0].m_myVar == null)myType[0].m_myVar = transform;
         create = false;
     }
 }

When I Edit The Script Or Recompile The Script It Will Work!!
Even If I Check The Length Or The Array It Self, It Doesn't Work:
if(myType) Or if(MyType != null) Or if(myType.Length > 0) Or if(myType.Length != 0), None Of Them Work!
But In This Way It Works Fine, But I Have To Click The Button 2 Times To Make It Work:
if(myType[0]) Or if(myType[0] != null).
Thanks...

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by bobisgod234 · Aug 10, 2017 at 07:22 AM

 if(myType.Length == 0)myType = new MyClass[1];

You create a new array of MyClass that is 1 long. Without assigning any instances of MyClass to that array, it will just be filled will null.

 if(myType[0].myVar == 0)myType[0].myVar = 1;
 

Here, you try to access the first element of myType, which is still null as you never assigned anything to it. You need to assign instances of classes to references for them to work.

Replace

 if(myType.Length == 0)myType = new MyClass[1];

with

 if (myType.Length == 0) myType = new MyClass[1] { new MyClass() };

Now you will create a new MyClass array that actually contains a live instance of MyClass, instead of null.

Comment

People who like this

0 Show 3 · 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 $$anonymous$$ · Aug 10, 2017 at 07:27 AM 0
Share

Your Solution Only Works In C# :D, I Need Both, I Think JavaScript Can't Create new Array Classes Like C#, The Problem Is With The Brackets.

avatar image bobisgod234 $$anonymous$$ · Aug 10, 2017 at 07:36 AM 0
Share

Why do you need both?

Then replace

 if(myType.Length == 0)myType = new MyClass[1];
 

With

          if(myType.Length == 0)
          {
             myType = new MyClass[1];
             myType[0] = new MyClass();
          }
avatar image $$anonymous$$ bobisgod234 · Aug 10, 2017 at 07:41 AM 0
Share

My Assets And Packages That Get Uploaded Have To Be In Both Languages To Be Readable And Used By Everyone, Also Thank You So Much, It Worked!

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Instantiating gameobjects in an array / class problem | NullReferenceException: 0 Answers

adding classes to arrays 2 Answers

Checking array fails each time. But change anything and resave and it works until unity closes...... 1 Answer

Problem with Singleton and NullReferenceException? 0 Answers

Why ExecuteInEditMode Always Causes NullReferenceException Errors Even In Clamping!! 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