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 jim1608 · May 31, 2014 at 02:50 AM · mesharraylistcolorrenderer

Array of Colors not working.

I'm trying to make a puzzle game where the tiles are generated with random colors at game start.

What I'm trying to do is create a Color[] array, store the eligible colors in it and then for every tile object choose a random color and change the mesh color to it.

For some reason though Unity keeps saying that the "array index is out of range".

Can someone tell me what dumb mistake am I making here?

var colors:Color[];

 function Awake(){
     colors[0]= Color.blue;
     colors[1]= Color.red;
     colors[2]= Color.green;
     colors[3]= Color.yellow;
     colors[4]= Color.cyan;
     colors[5]= Color.magenta;
 }
 
 function Start () {
     var colornum:int = Random.Range(0,3);
     renderer.material.color = colors[colornum];
 }

Am I creating the array wrong? I CAN do this creating a list and adding the colors to it, what I want is to understand why doing THIS way doesn't work. I already created the list and I'm gonna keep using that method anyway.

Comment
Add comment · Show 1
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 Owen-Reynolds · May 31, 2014 at 04:56 AM 0
Share

In C#, you have to create the array (in Awake): colors = new Color[6]; I thought javascript made the array for you, but I guess not (#pragma strict.)

But, it's often easier to make it public, then just enter colors in the Inspector.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · May 31, 2014 at 06:21 AM

Like Owen said you have to actually create the array. A native-array has a fix size which has to be specified when you create the array which you don't.

In Awake add this line infront of your others:

     colors = new Color[6];

btw: In line 11 you select a random color but only from the first 3 colors, is that intentional? Usually you would do something like:

     var colornum = Random.Range(0, colors.Length);
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 Eric5h5 · May 31, 2014 at 06:20 AM

Public variables take their values from the inspector, not your code. You should assign the size of the array and the colors in the inspector. If you want to do it in code, use private variables. Also use the length of the array in Random.Range, don't hard-code numbers.

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

23 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

Related Questions

Changing two different objects renderer colour 1 Answer

How to Make Imported MagicaVoxel 3D Object Flash White 0 Answers

A node in a childnode? 1 Answer

Data overriding itself? 2 Answers

how to assign mesh.vertices ToList linq extension 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