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 /
avatar image
0
Question by JK3D · May 23, 2014 at 05:50 PM · animationarraymaterialcolor

Array breaking when pushing floats into it via a for loop.

so I'm trying to figure out a chunk of code that is supposed to grab a group of objects by tag, and then create a an array of material colors to re-assign to their respective materials as a pulsing animation. the code was designed this way so any object tagged "pulse" would be capable of pulsing without any other changes made to it.

I get a clean count of 95 when I run the code, so I know it's finding the objects and adding them to the first array. it's when it tries to add the material colors to the second array when it breaks. I cannot find any examples of this error code online for js.

 IndexOutOfRangeException: index
 System.Array.System.Collections.IList.get_Item (Int32 index) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System/Array.cs:303)
 Boo.Lang.Runtime.RuntimeServices.GetArraySlice (System.Object target, System.Object[] args)
 Boo.Lang.Runtime.RuntimeServices.GetSlice (System.Object target, System.String name, System.Object[] args)
 Boo.Lang.Runtime.DynamicDispatching.SliceDispatcherFactory+<CreateGetter>c__AnonStorey12.<>m__5 (System.Object o, System.Object[] arguments)
 Boo.Lang.Runtime.RuntimeServices.GetSlice (System.Object target, System.String name, System.Object[] args)
 Pulse.Awake () (at Assets/Scripts/Pulse.js:57)


the code seemingly causing the bug is below:

 private var objects : Array ;
 private var objColor : Array ;
 private var matNum : int = 1 ; // for development: materials[matNum] ... most will use 1, simple objects will use 0
 
 private var LightsCentral : LightsoutCentral ;
 
 function Awake() 
 {
     stageDurations = Array ( pulseTurnOnDuration, pulseStayOnDuration, pulseTurnOffDuration, pulseStayOffDuration ) ;
     objects = GameObject.FindGameObjectsWithTag ("Pulse") ;
     print(objects.length);
     objColor = new Array() ;
     if ( objects.length > 0 ) 
     {
         for ( var i = 0 ; i < objects.length ; i++ ) 
         {
             objColor.Push (objects[i].renderer.materials[matNum].color.a) ;
             }
     }
     print(objColor);
     LightsCentral = GameObject.Find ( "ScriptStorage" ) . GetComponent ( "LightsoutCentral" ) ;
 }


any help identifying what needs fixed would be greatly appreciated!

JK

Comment
Add comment · Show 5
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 SuperMasterBlasterLaser · May 23, 2014 at 05:53 PM 0
Share

$$anonymous$$aybe you should use ArrayList(C#):

 ArrayList a = new ArrayList();
 
 a.Add(YOUR_THINGS_THAT_YOU_WANT);
avatar image Eric5h5 · May 23, 2014 at 06:00 PM 0
Share

You don't want to use ArrayList or JS Array. They are both slow, untyped, and obsolete. You should use a generic List ins$$anonymous$$d. (Not that this has anything to do with the code in the question.)

avatar image Jeff-Kesselman · May 23, 2014 at 06:01 PM 0
Share

The top slot of an array in array length -1.

e.g. if an array has 2 slots, the slots are [0] and [1]

avatar image Landern · May 23, 2014 at 06:02 PM 0
Share

Shouldn't use unity build in Array class or ArrayList either, no design time type checking, Use a List or Collection.

In the case above put a break point in the for loop and step through your code, check objects[i] along with the material[matNum], because the for loop is based off the items in the objects Array, it shouldn't be throwing and exception, but matNum may be incorrect for the materials array. I would guess you are trying to pull a material from the materials array and it doesn't exist as an item by index.

avatar image Jeff-Kesselman · May 23, 2014 at 09:51 PM 0
Share

You are all thinking too hard...

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jeff-Kesselman · May 23, 2014 at 09:52 PM

This is your problem...

 for ( var i = 0 ; i < objects.length ; i++ ) 

Array indices go from 0 to array length -1, so this should be

  for ( var i = 0 ; i < objects.length-1 ; i++ ) 
Comment
Add comment · 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
0

Answer by JK3D · May 23, 2014 at 11:03 PM

so far, no dice with any of these... I tried switching the array to a list (for the list, used float, realized my mistake, switched to vector4, didn't work either, settled on color and it worked, kind of)

the error went from that long debug error to a short one saying I was miscasting, and once I got it casting properly.... back to the same big error again.

thanks anyhow everybody. at times like this I wish I was a programmer, not a designer.

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

Changing two different objects renderer colour 1 Answer

Material doesn't have a color property '_Color' 4 Answers

can't animate MeshRenderer.Material._Color property 1 Answer

Can't animate Material.Color properties 3 Answers

How to change the color of a material in an array at run time 3 Answers


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