- Home /
I need help with Javascript
I have a script but the script is for when I walk over to my gun and the gui pops up, how do i get rid of the gui by either pressing 2 or walking away
 var message = false;
 
               function OnTriggerEnter (other : Collider) {
 message = true; }
 function OnGUI () { if (message){ GUI.Box (Rect (600, 350, 250, 40), "Press 2 to take this gun"); } } 
               Comment
              
 
               
              Answer by DynamicPrgm · Mar 19, 2013 at 01:57 PM
You could use the OnTriggerExit function and use a var for input;
 private var message = false;
 private var gunTaken = false;  //A variable for if you have taken the gun
 
 function OnTriggerEnter(other : Collider){ 
 message = true; 
 }
 
 function OnTriggerExit(other : Collider){  //if we exit the collider we cant pick it up
 message = false;
 }
 
 function OnGUI () {
      if (message && !gunTaken){  //if you haven't taken the gun
      GUI.Box (Rect (600, 350, 250, 40), "Press 2 to take this gun");
           if(Input.GetKeyDown(KeyCode.2){  //if you take the gun it is taken
           gunTaken = true;
           }
      }
 }
 
Answer by Landern · Mar 19, 2013 at 01:58 PM
OnTriggerExit, set message to false.
 var message = false;
  
 function OnTriggerEnter (other : Collider) {
     if (!message){
         message = true; 
     }
 }
 
 function OnTriggerExit (other : Collider) {
     if (message){
         message = false;
     }
 }
 
 function OnGUI () {
     if (message){
         GUI.Box (Rect (600, 350, 250, 40), "Press 2 to take this gun");
     }
 }
Your answer
 
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Dart/Bullet comes out pointing up 1 Answer
Get a gun off a wall 1 Answer
My gun wont fire ! 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                