- Home /
 
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
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....
Perhaps you want to use another boolean to represent showGUI and then check that in OnGUI?
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...
...additional, I added Debug.Log("GUI"); in that if loop. It is working, but it does not show times o.O
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.
Your answer
 
             Follow this Question
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