- Home /
 
Need help with game points and trigger
Hello!
How do I make the scoring (1 second = 1 point)? I have to do so that when the player touched the trigger, time stopped and the screen shows the number of points.
Here is my script:
 private var startTime :float;
 private var textTime : String;
 var G : boolean = false;
 var S : boolean = false;
 var B : boolean = false;
 var F : boolean = false;
 var other : GameObject;
 
 function Awake() {
 
    startTime = Time.time;
 
 }
 
 function OnGUI () {
 
    var guiTime = Time.time - startTime;
 
    var minutes : int = guiTime / 60;
    var seconds : int = guiTime % 60;
 
    textTime = String.Format ("{0:00}:{1:00}", minutes, seconds); 
    GUI.Label(Rect (Screen.width/2, 0, 100, 100), textTime); 
  
    if(seconds >= 10){
        G = true;
        }
    
        if(seconds >= 30){
            G = false;
            S = true;
        }
        
            if(seconds >= 90){
                G = false;
                S = false;
                B = true;
            }
            
                if(seconds >= 100){
                    G = false;
                    S = false;
                    B = false;
                    F = true;
                }
 }
 
 function OnTriggerEnter (other : Collider) {
 
    
 
               The Problem here
 }
 
               You should really make a better 'Questions topic name' "Need help!" is really a bad name as it doesn't tell the forum anything.
Answer by Paul-Sinnett · Oct 25, 2012 at 12:49 PM
You'd need to add a score variable. If you want to add 1 point per second, add Time.deltaTime to score within function Update (). If you want to start and stop the timer, make a boolean to store when you are scoring and only add when that variable is true. Then inside the OnTriggerEnter function, set the flag to false.
Answer by Sonaten · Oct 25, 2012 at 07:02 PM
I am not really native in Javascript, but give this a try. You simply have a control variable, in this case a bool, which controls wether or not a certain part of the code is executed. I assume that you want the calculations to stop.
Hope this can help you out.
 private var startTime :float;
 private var textTime : String;
 var G : boolean = false;
 var S : boolean = false;
 var B : boolean = false;
 var F : boolean = false;
 var other : GameObject;
 
 //Edited here. isRunning added and 3 vars moved to outer scope
 var isRunning : boolean = true;
 
 var guiTime;
 var minutes : int;
 var seconds : int;
 
 function Awake() {
 
    startTime = Time.time;
 
 }
 
 function OnGUI () {
 
     //Calculations. Stop them if trigger is hit 
     if (isRunning)
     {
        guiTime = Time.time - startTime;
        minutes = guiTime / 60;
        seconds = guiTime % 60;
        }
 
    textTime = String.Format ("{0:00}:{1:00}", minutes, seconds); 
    
    GUI.Label(Rect (Screen.width/2, 0, 100, 100), textTime); 
 
    if(seconds >= 10){
     G = true;
     }
 
        if(seconds >= 30){
            G = false;
            S = true;
        }
 
           if(seconds >= 90){
              G = false;
              S = false;
              B = true;
           }
 
             if(seconds >= 100){
               G = false;
               S = false;
               B = false;
               F = true;
             }
 }
 
 function OnTriggerEnter (other : Collider) {
     if(other.tag == "triggerObjectTag") isRunning = false;
 }
 
              Your answer