Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Dec 17, 2015 at 01:59 AM by Krazeecain for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Krazeecain · Apr 02, 2013 at 11:30 PM · javascriptvariablearrayssyntax#pragma strict

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

Comment
Add comment · Show 3
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 sasuke908 · Apr 03, 2013 at 12:36 AM 1
Share

i think you missed the 'new' keyword. and screw the var in the array initializer.

try

 var spectrum : float[] = new float[64];
avatar image Eric5h5 · Apr 03, 2013 at 12:44 AM 0
Share

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.

avatar image Loius · Apr 03, 2013 at 12:44 AM 0
Share

never use Array for anything. use List. ins$$anonymous$$d, because it is better in literally every way.

var myList : List. = new List.();

1 Reply

  • Sort: 
avatar image
2
Best Answer

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( ... );

Reference: http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?#Javascript_Arrays


 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];

Comment
Add comment · Show 1 · 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 Krazeecain · Apr 03, 2013 at 01:59 AM 0
Share

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

Answers Answers and Comments

14 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

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


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