- Home /
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.
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.
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!
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
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];
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.
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
Follow this Question
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