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
1
Question by kylehlogan · Oct 01, 2012 at 01:20 AM · colorfade

Problem with fading a colour over time

Hi, I'm having some problems with the following code, the '/ fadetime * Time.detlaTime'' part to be specific. I wrote it after looking at the light.color documentation and the example that shows how to fade a colour over a set time.

         selection = lightcolour [Random.Range(0, lightcolour.length)]; 
         light.color = selection / fadetime * Time.deltaTime;

It works perfectly fine if I want to change the colour instantly but I can't fade it over a set time. I need to keep the 'light.color = selection' part because other scripts rely on the selection variable.

I get this error, I understand what it means but I just can't think of any other ways of doing this,

'Operator '/' cannot be used with a left hand side of type 'Object' and a right hand side of type 'int'.'

Comment
Add comment · Show 2
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 kylehlogan · Oct 01, 2012 at 01:38 AM 0
Share

I can't get either of them to work, it's just all co$$anonymous$$g back to problems with not being able to use floating points or integers.

avatar image kylehlogan · Oct 01, 2012 at 02:00 AM 0
Share

I get 'Cannot convert 'float' to 'UnityEngine.Color'.' Is it possible that this is just something that I can't do with Unity really?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Oct 01, 2012 at 02:21 AM

It would be better to use Color.Lerp(color1, color2, t): this returns a color interpolated between the two colors passed, according to t (0=color1, 1=color2).
The code below sets immediately a random color when SetColor is called, and this color fades to the endColor over time during fadeTime seconds:

 var endColor = Color.black;
 var fadeTime: float = 2.5;
 var selection: Color;
 var lightcolour: Color[];

 private var tColor: float;
 
 function SetColor(){
   selection = lightcolour [Random.Range(0, lightcolour.length)];
   tColor = 0; // reset timer
 }
 
 function Update(){
   if (tColor <= 1){ // if end color not reached yet...
     tColor += Time.deltaTime / fadetime; // advance timer at the right speed
     light.color = Color.Lerp(selection, endColor, tColor);
   }
 }
 

PS: The error you've encountered at first was probably caused by selection being untyped: when the compiler can't deduce the variable type, it assumes the object type, which can hold any standard type.

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

12 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

Related Questions

Shuriken particle color over lifetime won't fade my particles? 3 Answers

How to make a gameObject's color fade to transparency (picture) 3 Answers

Lerping/Fading between multiple colours 1 Answer

Changing Alpha Thru Code Acts Weird 1 Answer

Material doesn't have property '_Color' 4 Answers


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