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
0
Question by VVarlock · Mar 26, 2013 at 05:52 PM · triggerongui

Problem with Colider/Trigger and OnGUI function

This is related to link text

But somehow I cannot ask another question there.

So here it is. Two scripts are a bit changed.

TimingTriggers.js

 enum TriggerType 

 {

     start, split1st, split2nd

 }

 

 var triggerType : TriggerType;





and TimedItem.js

 public var sectionShow : float = 0.0f;
 var myTime : float = 0.0f;
 
 private var startCount : boolean = false;
 private var section1st : boolean = false; 
 private var section2nd : boolean = false;
 private var sectionTemp : boolean = false;
 
 private var myLapTime : float = 0.0f;
 
 
 private var iMinutes : int;
 private var iSeconds : int;
 private var iTens : int;
 private var iHundreths : int;
 private var iThousenths : int;
 var LapTime : String;
 private var OldLapTime_1 : String;
 private var OldLapTime_2 : String;
 private var OldLapTime_3 : String;
 var OldLapTime_temp : String;
 
 
 function Update () {
 
     // If new lap is started, reset all, preserve old lap times
     if(startCount)
     {
         sectionTemp = true;
 
         OldLapTime_3 = OldLapTime_2;
         OldLapTime_2 = OldLapTime_1;
         OldLapTime_1 = LapTime;    
                 
         myLapTime = 0.0f;
         startCount = false;
         section1st = false;
         section2nd = false;
     }
     
     // Time calculation
     if(sectionTemp)
     {
         // Time counter
         myLapTime += Time.deltaTime;
         
         iMinutes = (myLapTime/60f);
         iSeconds = (myLapTime%60f);
         iTens = ((myLapTime*10)%10);
         iHundreths = ((myLapTime*100)%10);
         iThousenths = ((myLapTime*1000)%10);
         LapTime = String.Format("{0:00}:{1:00}:{2:0}{3:0}{4:0}",iMinutes,iSeconds,iTens,iHundreths,iThousenths);
     }
 }
 
 // This is funciton from your post, but some things are added
 function OnTriggerEnter(other : Collider)
 {
     var timing = other.GetComponent(TimingTriggers);
     if(timing)
     {
         switch(timing.triggerType)
         {
             
             //Car passed Start Line
             case TriggerType.start:
             Debug.Log("Start");
             startCount = true;
             break;
             
             //Car passed 1st split time Line
             case TriggerType.split1st:
             Debug.Log("1st");
             section1st = true;
             break;
             
             //Car passed 2nd split time Line
             case TriggerType.split2nd:
             Debug.Log("2nd");
             section2nd = true;
             break;
 
             
         }
     }
 }
 
 // This script is showing Lap Times on GUI
 function OnGUI()
 {
     GUI.Box (new Rect(Screen.width/2-120,5,240,50), "");
     GUI.Label(new Rect(Screen.width/2-110,5,100,20), "" + LapTime);
     if(startCount)    // HERE IS PROBLEM
     {
         GUI.Label(new Rect(Screen.width/2,5,100,20), "" + OldLapTime_1);
         GUI.Label(new Rect(Screen.width/2,20,100,20), "" + OldLapTime_2);
         GUI.Label(new Rect(Screen.width/2,35,100,20), "" + OldLapTime_3);
     }
 }
 



Problem is in last part of OnGUI function, it depends of Trigger, but when triggered it shows nothing on GUI.

Cheers

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 whydoidoit · Mar 26, 2013 at 06:29 PM

In your Update function (which runs every frame) you do this:

 if(startCount)
 {
    sectionTemp = true;
 
    OldLapTime_3 = OldLapTime_2;
    OldLapTime_2 = OldLapTime_1;
    OldLapTime_1 = LapTime;   
 
    myLapTime = 0.0f;
    startCount = false;
    section1st = false;
    section2nd = false;
 }

Which sets startCount to false immediately if it was true....

Comment
Add comment · Show 7 · 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 whydoidoit · Mar 26, 2013 at 06:29 PM 0
Share

BTW you should ask a second question in this case.

avatar image whydoidoit · Mar 26, 2013 at 06:30 PM 0
Share

Perhaps you want to use another boolean to represent showGUI and then check that in OnGUI?

avatar image VVarlock · Mar 26, 2013 at 06:43 PM 0
Share

But that part of code is executed only when object passes trigger which is related to startCount variable. I added another variable after 68th line, it is startGUI = true; and changed in line 93 startCount for startGUI, yet nothing happens. Also in that if loop I add at end startGUI = false;

Cheers...

avatar image VVarlock · Mar 26, 2013 at 06:45 PM 0
Share

...additional, I added Debug.Log("GUI"); in that if loop. It is working, but it does not show times o.O

avatar image whydoidoit · Mar 26, 2013 at 06:48 PM 0
Share

Stop turning it off immediately as you turn it on :)

If you put startGUI = false in that loop which runs every frame you will immediately turn it off again. You see one Debug.Log in the OnGUI for the one frame the GUI is visible - you should have one log per frame.

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

11 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

Related Questions

How to have OnGUI element in a OnTriggerEnter function? 3 Answers

OnGUI not working? 1 Answer

Adding a value after a trigger on 1 Answer

Can't click gameobject when over another trigger? 1 Answer

enable a canvas on trigger 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