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

  • Sort: 
avatar image
0
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
Add comment · 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 $$anonymous$$yClass[1];
 

With

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

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

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

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

Array.length edited using scripts 2 Answers

Not a member of Object..Can't get class.name from array 1 Answer

C# custom class array error in adding a new entry. 1 Answer

Pushing GameObject into JS Array returns 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