• 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
Question by vespera · Apr 09, 2016 at 01:55 PM · c#traffic

Traffic light turns green when the car reaches?

I'm new to Unity and using Unity to design a auto-driving police car for my project, I've had traffic lights that has 3 lights, now what I want is when the car reach the traffic light, if the light is red or yellow it should turn to green. I have wrote a script with OnTriggerEnter to change the light but the problem is the light turns green but other color is on at the same time. What I'm thinking is to pause the light function when the car reach the trigger, change the light with OnTriggerStay, and resume the light function with OnTriggerExit. Could anybody give hint how to pause a function? and is this a good way to do? Here is my script:

public class triggerenter : MonoBehaviour { public Light red; public Light yellow; public Light green;

 void   OnTriggerEnter()
 {
     if (green.enabled = true)
     {
         Debug.Log("Light is green");
       
     }
     else if (yellow. enabled = true)
     {
         yellow.enabled = false;
         green.enabled = true;
         Debug.Log("light change from yellow to green");
          
     }
     else
     {
         red.enabled = false;
         green.enabled = true;
         Debug.Log("light change from red to green");
         
     }
     }
 }

and the traffic light script: public class trafficlight : MonoBehaviour {

 public Light green;
 public Light yellow;
 public Light red;
 public float greentimer;
 public float redtimer;
 public float yellowtimer = 2;

 IEnumerator Start()
 {

     if (yellow != null)
     {
         yellow.enabled = false;

     }
     else { Debug.Log("yellow light not defined"); }


     while (true)

     {
         green.enabled = true;
         red.enabled = false;
         yield return new WaitForSeconds(greentimer);
         yellow.enabled = true;
         green.enabled = false;
         yield return new WaitForSeconds(yellowtimer);
         yellow.enabled = false;
         red.enabled = true;
         yield return new WaitForSeconds(redtimer);
     }
 }

}

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by Toon_Werawat · Apr 09, 2016 at 03:23 PM

Here. I got whole new script for you

 using UnityEngine;
 
 public class TrafficLight : MonoBehaviour
 {
     public Light greenLight;
     public Light yellowLight;
     public Light redLight;

     //Instead of make 3 light count. Just make only one.
     public float LightCount;
     public enum state
     {
         red = 0, yellow = 1, green = 2, yellow2 = 3
     }//Here is light state. Also. Reason why we have 2 yellow? Because enum can act. Like int. And so if we want to toggle to next state. We just use + 1 on it. And it will be weird if it just go from red to yellow to green. Then immediately red.
     public state LightState = state.yellow;
     //Light state. Default yellow.
     public bool emergency;
     //In case of emergency. Toggle.
 
     void Update()
     {
         if (!emergency) // Are we have emergency? If not. Do this.
         {
             LightCount -= Time.deltaTime; //Count lightCount down to 0
             redLight.enabled = LightState == state.red;
             yellowLight.enabled = LightState == state.yellow || LightState == state.yellow2;
             greenLight.enabled = LightState == state.green;
                     //This is a shorter version of 
                     /*if (LightState == state.red)
                         {
                             redLight.enable = true;
                             yellowLight.enable = false;
                             greenLight.enable = false;
                          }
                          if (LightState == state.yellow.....blah blah blah*/
                     //This is what we do. When light count to 0
             if (LightCount <= 0)
             {
                             //Add light state up. By 1
                 LightState += 1;
                             //It act like int. So it will go up more then 3 and we have to cap it down to 0. But it not really int. We have to cast it.
                 if ((int)LightState > 3)
                 {
                     LightState = 0;
                 }
                             //After we add LightState up. We start counting again. If we at yellow light? Give it 2 Seconds.
                 if (LightState == state.yellow || LightState == state.yellow2)
                 {
                     LightCount = 2;
                 }
                             //If we at red or green light? Give random number between 20 - 50 seconds
                 else if (LightState == state.red || LightState == state.green)
                 {
                     LightCount = Random.Range(20, 50);
                 }
             }
         }
         else //Do this if emergency.
         {
             greenLight.enabled = true;
             yellowLight.enabled = false;
             redLight.enabled = false;
         }
     }
     /When enter trigger.
     void OnTriggerEnter()
     {
         emergency = true;
     }
     //When leave trigger
     void OnTriggerExit()
     {
         emergency = false;
     }
 }






Comment

People who like this

0 Show 3 · 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 vespera · Apr 10, 2016 at 02:25 AM 0
Share

Thank you very much! This is much better than my method. May I ask one more question, in your code when the emergency is on the light does change to green but once the car exits the trigger it immediately changes to another color, I tried to add an Invoke function to delay the OntriggerExit for few seconds but it didn't work. Could you please let me know how to do it?

avatar image Toon_Werawat vespera · Apr 10, 2016 at 05:38 AM 0
Share

You can use.

Coruetine

 void OnTriggerExit()
 {
     StartCoruetine(RestoreEmergency());
 }

or Invoke

 void OnTriggerExit()
 {
     Invoke("RestoreEmergency",2f);
 }

And at "RestoreEmergency"

 void RestoreEmergency()
 {
     emergency = false;
 }
avatar image vespera · Apr 10, 2016 at 04:52 PM 0
Share

Ah, I didn't create a new function that's why. It works fine now, thanks a lot!

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

139 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

Related Questions

I cant make a stoplight delay from green to yellow, then to red, when my boolean is off. How can I make it delay and actually get yellow to show? 0 Answers

Need Help Fixing Camera 0 Answers

Difference between serialization and saving data 2 Answers

Controller script active on both characters. 1 Answer

Error in camera script 1 Answer


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