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 hijinxbassist · Mar 15, 2012 at 04:44 AM · arraypragmapragma strict

Pragma strict and arrays

Having a hard time changing a color to a color i have stored in an array. The line that throws the error is the one in my update.
(This is only a sample of code to make it easy to read)

 #pragma strict
 
 public var playerColors:Array=Array();
 public var currentColor:Color;
 
 function Update()
 {
     currentColor=playerColors[1];
 }

The error i am getting is this..

BCE0022: Cannot convert 'Object' to 'UnityEngine.Color'.

which makes sense, but im not sure how to correct it. I have tried using many methods, but still get this or other errors(based on the method used). Can someone please give me some insight as to what i am doing wrong and a solution. Thank you in advance.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by syclamoth · Mar 15, 2012 at 05:02 AM

Well, if you're not using any dynamic stuff, why not just use a builtin array and avoid that whole mess?

public var playerColors : Color[];

That way, your indexing will work properly.

By the way, the reason for your problem is that #pragma strict removes all dynamic typing. The practical upshot of this is that you can't just get a value out of an Array class- you need to cast it back to the type that you need it to be.

currentColor = (Color)playerColors[1];

However, Unity's Array class is slow and badly typed, so you're better off just using the builtin one.

If you need the ability to dynamically add and remove objects (but the objects will only ever be of a single type), you might be better off using

var playerColors : System.Collections.Generic.List.<Color> = new System.Collections.Generic.List.<Color>();

This has Add and Remove methods, as well as some other things you might find useful. If that name is a bit too long for you, add a directive importing System.Collections.Generic at the top of your file.

Comment
Add comment · Show 5 · 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 hijinxbassist · Mar 15, 2012 at 05:26 AM 0
Share

Funny thing, i tried this currentColor = playerColors[1] as Color; before and it would give me this error

BCE0006: 'UnityEngine.Color' is a value type. The 'as' operator can only be used with reference types. I guess cuz im using the Array().

The as operator saved me with the rest of this script, but not in this case.

I see what you are saying tho. The whole reason i was using the built in array was for the features. Im going to try the builtin array right now. Thank you!

avatar image hijinxbassist · Mar 15, 2012 at 05:37 AM 0
Share

I am still getting the error

BCE0006: 'UnityEngine.Color' is a value type. The 'as' operator can only be used with reference types.

Any idea as to why this is happening? Here is the actual line straight from my code. Should be easy enough to read as is.

playerBot.renderer.materials[0].color=playerColors[colorNumber]as Color;

For some reason it doesnt like me using the as operator

avatar image syclamoth · Mar 15, 2012 at 05:51 AM 0
Share

Ok, sorry. Looks like I don't know my JavaScript properly. I strongly recommend using a builtin array for this- but if that's not possible, try this syntax:

 (Color)playerColors[1];
avatar image syclamoth · Mar 15, 2012 at 06:28 AM 0
Share

Oh! You don't need to use the 'as' or cast if you're using builtin arrays. Just get rid of all that stuff! Just use

 currentColor = playerColors[1];

That should work. Yes, I usually use C#- I don't get silly dynamic typing problems.

avatar image hijinxbassist · Mar 15, 2012 at 06:38 AM 0
Share

HAHAHA! Wow, one thing i didnt expect to try at this point yet it completely makes sense! Im planning on taking up C# once i can master unitys java. Thanks a bunch for your help, im suprised i didnt relize this before! Thanks again, happy trails.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

pragma strict create List of Array of strings for class? 1 Answer

Custom Sort class array error if i use #pragma 2 Answers

Implicit Downcast Warning and Converting Arrays 2 Answers

Downcasting Array to int (without #pragma downcast) 1 Answer

Adding a Gameobject to an array makes all elements of that array equal to the object 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