x


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.

more ▼

asked Mar 15 '12 at 04:44 AM

hijinxbassist gravatar image

hijinxbassist
2.1k 23 31 38

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Mar 15 '12 at 05:02 AM

syclamoth gravatar image

syclamoth
15k 7 15 80

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!

Mar 15 '12 at 05:26 AM hijinxbassist

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

Mar 15 '12 at 05:37 AM hijinxbassist

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];
Mar 15 '12 at 05:51 AM syclamoth

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.

Mar 15 '12 at 06:28 AM syclamoth

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.

Mar 15 '12 at 06:38 AM hijinxbassist
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1396
x33
x9

asked: Mar 15 '12 at 04:44 AM

Seen: 1030 times

Last Updated: Mar 15 '12 at 06:38 AM