x


The problem about Color In C#!

I got a script with C#.For example: This is one function in the script.

 void OnShow(){
   Color C1=new Color(0.5,0.5,0.5,0.5);
   Color C2=new Color(0.75,0.75,0.75,0.75);

   LineRenderer LineRender=(LineRenderer)GetComponent(LineRenderer);
   LineRender.material=new Material("Particle/Additive");

`` LineRender.SetColors(C1,C2);
}

It Shows errors:
err1502:The best OverLoadedmethod match for "UnityEngine.Color.Color(float,float,float,float)" has some invalid arguments;
err1503:Argument "#1" cannot convert "double" expression type "float".

why do those errors happen?
"0.5" should be "float"! Is it "Double"?
I can not get it?
Please help me....
I am very tangle....
more ▼

asked May 11 '12 at 03:40 AM

narutowt gravatar image

narutowt
-10 2 6 7

0.5 is a double literal, just like "Hello" is a string literal! To make the language interpret it as a float (which has a lower precision, so it's right to stop you from doing this accidentally), you need to specify 'f' at the end. Hence:

float whatev = 0.5f;

(it's called a 'double' because it's twice the size of an average float in memory- and hence, about twice the precision)

May 11 '12 at 03:45 AM syclamoth

Thanks,I got it.I almost forget this tips.

May 11 '12 at 05:59 AM narutowt

@syclamoth It's not twice the precision. It's potentially 4,294,967,296 times the precision! However, .NET/Mono uses some of the bits for extra size, and some of the bits for extra precision. Still, you get 256-512 times the precision, much more than 2x!

Aug 07 '12 at 03:02 AM Jessy
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

float = 1.0

more ▼

answered May 11 '12 at 03:41 AM

Lo0NuhtiK gravatar image

Lo0NuhtiK
3.5k 1 9 39

haha epic

Aug 07 '12 at 04:21 AM flamy
(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:

x4167
x507

asked: May 11 '12 at 03:40 AM

Seen: 865 times

Last Updated: Aug 07 '12 at 04:21 AM