The question is answered, right answer was accepted
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?
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.
i updated the question, the error occurs in the Clamping line when i clamp the customizationID between 0 to Length of the class(meshGroups)
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.
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.
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
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
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