Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by brandon_lapinska · Oct 30, 2016 at 04:42 PM · c#timercolor changetime.deltatime

Changing Color of Object Using a Timer

I am having a issue creating a timer that changes the color of my traffic light. This is the code that I currently have. I have two different scripts. One for a State Machine and one for the Lights.

     private bool isGreen = true;
     private bool isYellow = true;
     private bool isRed = true;
 
     private float timePassed = Time.deltaTime ++;
 
 
     bool IsLightGreen()
     {
         return isGreen;
     }
 
     bool IsLightYellow()
     {
         return isYellow;
     }
 
     bool IsLightRed()
     {
         return isRed;
     }
 
     void ToggleLight()
     {
         isGreen = !isGreen;
         isYellow = !isYellow;
         isRed = !isRed;
     }
 }

I am probably going all about this wrong but I keep researching time and color changing and all I can find is posts about using java script which I have a hard time reading and translating it to C#. Any suggestions would be great. Keep in mind I am super beginner and am in college still learning C#.

Thanks

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 Cynikal · Oct 30, 2016 at 06:07 PM

A simple timer:

 float _t = 0f;
 
 void Update()
 {
 _t += Time.deltaTime;
 
 if (_t >= TheNumberOfSecondsIWant)
 {
 _t = 0f;
 TriggerTheLight();
 }
 }

I would actually probably set it up to trigger a coroutine, that i'd have cycle through the red/yellow/green light.

Comment
Add comment · Show 11 · 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 brandon_lapinska · Oct 30, 2016 at 06:13 PM 0
Share

It has to go from green to yellow to red... I will try to implement what you showed me. $$anonymous$$aybe my brain will start working correctly lol.

avatar image Cynikal ♦ brandon_lapinska · Oct 30, 2016 at 06:20 PM 0
Share

If you change per my example:

_t = 0f; TriggerTheLight();

to:

_t = 0f; StartCoroutine(TriggerTheLight());

You could then do:

 IEnumerator TriggerTheLight() {
     GreenLight.Enabled = false;
     YellowLight.Enabled = true;
     RedLight.Enabled = false;
 yield return new WaitForSeconds(1f);
 GreenLight.Enabled = false;
     YellowLight.Enabled = false;
     RedLight.Enabled = true;
 }
avatar image brandon_lapinska Cynikal ♦ · Oct 30, 2016 at 06:33 PM 0
Share

Should i get rid of the bool's?

Show more comments
avatar image Cynikal ♦ brandon_lapinska · Oct 30, 2016 at 08:06 PM 0
Share

First off, your timer is wrong.

if (_t >= TheTimeInSecondsYouWantGoesHere) { _t = 0f; StartCoroutine(TriggerTheLight()); }

As far as the Green/yellow/redlight.enabled stuff,

You need to declare variables.

public GameObject GreenLight; public GameObject YellowLight; public GameObject RedLight;

then assign them to their respective objects.

The purpose of Unity Answers is not for someone else to write your script for you, but to point you in the right direction. You'll need to modify and adapt the code accordingly.

avatar image brandon_lapinska Cynikal ♦ · Oct 30, 2016 at 08:08 PM 0
Share

Well I know Im not supposed to have someone write the code for me. I did learn quite a bit going back and forth. I will get this done thanks for the help.

Show more comments

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

254 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

,Why isn't the clock speed being affected by the float "clockSpeed"? 0 Answers

Time.deltaTime timer counting slowly 3 Answers

Disable Ragdoll #C - Timer before disable. 2 Answers

Respawn issues 2 Answers

Can somebody help me fix this script? 2 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