- Home /
The question is answered, right answer was accepted
Help with #pragma strict Arrays?
I'm trying to make an array in javascript. Usually this is a simple affair but I'm trying to dev for android (Ouya specifically) so I have to use "#pragma strict". Unfortunately this seems to turn arrays into gameobjects no matter what I do!
If I call a normal array like so:
var spectrum = new Array();
Then Unity thinks it's a gameobject type later on in my script. After some searching around, I discovered why Unity does this, but my attempts to resolve this issue don't seem to work.
If I try to instantiate a native .NET array like so:
var spectrum : float[];
Unity returns the error message: "Object reference not set to an instance of an object" and points to where I used my array again. This message doesn't pop-up until I run the game.
If I try to add in the length of the array (I think that's how it works?):
var spectrum = float[64];
Then Unity says there's no semi-colon. I think this is just a syntax error but there's an example somewhere in the docs that recalls an array like this.
I'm pulling my hair out right now! I've tried all sorts of other weird combinations but Unity absolutely REFUSES to recognize my array as an array! Please help me T_T
i think you missed the 'new' keyword. and screw the var in the array initializer.
try
var spectrum : float[] = new float[64];
Unfortunately this seems to turn arrays into gameobjects no matter what I do!
No, the issue is that Array is untyped, so anything in an Array is Object, not "gameobject". In any case, don't ever use the Array class...for dynamic-sized arrays, use a generic List, and if the array is a fixed size, then use built-in arrays.
never use Array for anything. use List. ins$$anonymous$$d, because it is better in literally every way.
var myList : List. = new List.();
Answer by Chronos-L · Apr 03, 2013 at 12:45 AM
var spectrum = new Array();
It is not gameObject type, it is Object
type. Whenever you need a specific function associated to a class, you need to cast the element in the array to use it.
var list = new Array();
list.Add( transform );
( (Transform) list[i] ).Translate( ... );
var spectrum : float[];
This line of code has no problem, it is usually used to assign values in the inspector. "Object reference not set to an instance of an object" occurs when you run the game is because you neither assign any value to it in the inspector, nor initiate it in your Start()
.
How to work with this
1. Assign the values in the inspector
When you assign at least one values to the spectrum in the inspector, an array will be initiated to store the value
2. Initiate the array in Awake or Start
void Awake(){
spectrum = new float[32];
}
var spectrum = float[64];
Plain typo error, the correct syntax should be:
var spectrum = new float[64];
var spectrum : float[] = new float[64];
Thanks for the help. I'm still getting the "Object reference not set to an instance of an object" error, but I've managed to get my code working the way I want it (Audio visualizer). The weird thing is that I only get one instance of that error, when there's 3 game objects with the script attached. :/ Ah well... I'm sure I'll figure it out eventually.
Follow this Question
Related Questions
how can I add the value of the same variable from different instances of a GameObject? 1 Answer
How to use Enum? 1 Answer
Public 2D array, accessed through the editor ?? 3 Answers
Changing The Value Of A Variable From Another Script (JavaScript) 1 Answer
When i call some variables to an other second script i have a problem 1 Answer