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 /
  • Help Room /
This question was closed Jun 24, 2017 at 01:08 PM by gtav225 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by gtav225 · Jun 24, 2017 at 05:11 AM · javascriptarrayarraysreadlength

Why i can't read a Variable's Length(Array)

I have been disappointed in using this for alot of times:

 @script AddComponentMenu("Utility/Customization System")
 @script ExecuteInEditMode()
 public var executeInEditMode : boolean = false;
 public var customizationMeshes : CustomizationMeshes[];
 public class CustomizationMeshes {public var CustomizationID : int = 0;public var meshGroups : MeshGroups[];}
 public class MeshGroups {public var SingleMeshes : GameObject[];public var GroupMeshes : GameObject;}
 public function Update ()
 {
     if(executeInEditMode || !executeInEditMode && Application.isPlaying)
     {
         for(var a : int = 0; a < customizationMeshes.Length; a++)for(var b : int = 0; b < customizationMeshes[a].meshGroups.Length; b++)for(var c : int = 0; c < customizationMeshes[a].meshGroups[b].SingleMeshes.Length; c++)
         {
             customizationMeshes[a].CustomizationID = Mathf.Clamp(customizationMeshes[a].CustomizationID,0,customizationMeshes[a].meshGroups[b].Length);
             if(customizationMeshes[a].CustomizationID == 0)
             {
                 if(customizationMeshes[a].meshGroups[b].SingleMeshes[c])customizationMeshes[a].meshGroups[b].SingleMeshes[c].SetActive(false);
                 if(customizationMeshes[a].meshGroups[b].GroupMeshes)customizationMeshes[a].meshGroups[b].GroupMeshes.SetActive(false);
             }
             if(customizationMeshes[a].CustomizationID > 0)
             {
                 if(customizationMeshes[a].CustomizationID - 1 != b)
                 {
                     if(customizationMeshes[a].meshGroups[b].SingleMeshes[c])customizationMeshes[a].meshGroups[b].SingleMeshes[c].SetActive(false);
                     if(customizationMeshes[a].meshGroups[b].GroupMeshes)customizationMeshes[a].meshGroups[b].GroupMeshes.SetActive(false);
                 }
                 if(customizationMeshes[a].CustomizationID - 1 == b)
                 {
                     if(customizationMeshes[a].meshGroups[b].SingleMeshes[c])customizationMeshes[a].meshGroups[b].SingleMeshes[c].SetActive(true);
                     if(customizationMeshes[a].meshGroups[b].GroupMeshes)customizationMeshes[a].meshGroups[b].GroupMeshes.SetActive(true);
                 }
             }
         }
     }
 }

it always sends an error message and doesn't work:
NullReferenceException: Object reference not set to an instance of an object. in line 13 (when clamping customizationID)
can someone help me with this?

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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by FlaSh-G · Jun 23, 2017 at 07:21 PM

This code is really weird. On line 13, we have

 customizationMeshes[a].meshGroups[b].Length

which should cause a compilation error before anything. You should always add

 #pragma strict

to the beginning of all UnityScripts, or switch to C#, then you'd probably get different error messages.

The thing with this line is that customizationMeshes[a] is a "CustomizationMeshes" object. So customizationMeshes[a].meshGroups[b] is a "MeshGroups" object. The class "MeshGroups" does not have a variable named "Length", so this line wouldn't even compile with #pragma strict.

You should perhaps fix this kind error (with #pragma strict, the compiler will help you finde them), use some more local variables to increase readabilty of your code and then have a look at it again.

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 gtav225 · Jun 23, 2017 at 07:39 PM 0
Share

i updated the question, the error occurs in the Clamping line when i clamp the customizationID between 0 to Length of the class(meshGroups)

avatar image gtav225 · Jun 24, 2017 at 01:08 PM 0
Share

oh my bad, my scripting is so much fast that even i make a small mistake, it had to be: costomization$$anonymous$$eshes[a].meshGroups.Length INSTEAD of costomization$$anonymous$$eshes[a].meshGroups[b].Length which Unity thinks that there is a variable called Length inside meshGroups, but i got the idea from your explanation and fixed it, so i set your answer as correct one, and close the Question.

avatar image
-1

Answer by Vicarian · Jun 23, 2017 at 07:25 PM

myVar is declared as a float array in your script, but doesn't yet have a value, which causes the interpreter to throw a NullReferenceException. In C#, the initializer is

 float[] myVar = float[arraySize];  

Where arraySize is a literal integral value. Looks like Unity Technologies added a way to make JS less inferior, so you can initialize the array with

 myVar = new float[arraySize];

again, where arraySize is a literal integral value. It appears you can't declare and initialize on the same line.

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 gtav225 · Jun 23, 2017 at 07:34 PM 0
Share

actually this one was an example, im using something else (i updated the Question) im trying to clamp customizationID between 0 and max length of that class(meshGroups) but it sends me an error

avatar image FlaSh-G · Jun 24, 2017 at 10:24 AM 0
Share

This anwer is incorrect. Since this is UnityScript, where public is the default visibility, Unity will deserialize whatever has been set up in the editor once the game is started.

Serialized fields, including myVar in this example, always have a value when starting the game.

Follow this Question

Answers Answers and Comments

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

how to not repeat random array 1 Answer

Why UNITY Hates Me By Reading Array's Length! 1 Answer

how to not repeat random array 0 Answers

Getting an error when using an Array of String Arrays 0 Answers

condition for array of textures 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