- Home /
Question by
ScottyKal · Apr 30, 2015 at 06:22 PM ·
guiupdateloopif-statementssendmessage
Why is OnGUI not working?
OnGui in not working if I have the Hover function updated in Update. The print shows that showLabel is changing from false to true as I hover over the object but the label will not come up. Here are the two scripts that are talking to each-other:
Interactee.js
var Interactable : boolean = true;
private var showLabel : boolean;
function Update(){
print (showLabel);
Hovers(false);
}
//Is the player interacting with the this object???
function Interact(Interacting : boolean){
if (Interactable){
if (Interacting){
//Action();
}
}
}
function Hovers (Hovering : boolean){
if(Hovering){
showLabel = true;
}else{
showLabel = false;
}
}
//The Action you want to happen...
/*function Action(){
//Make the item a child of backpack...
this.transform.parent = backpack.transform;
}*/
//While hovered show something...
function OnGUI(){
if (showLabel){
GUI.Label (Rect (Screen.width*0.5,Screen.height*0.65,100,50), "Hovering");
}
}
Interactor.js
var distance = 10;
var Interacting : boolean = true;
var Hovering : boolean = true;
//When "Interact" button is pressed do function Interact...
function Update(){
Hover();
if (Input.GetButtonDown("Interact")){
Interact();
}
}
//Raycast
function Interact(){
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 20));
if (Physics.Raycast (ray, hit, distance)){
hit.transform.SendMessage("Interact", Interacting, SendMessageOptions.DontRequireReceiver);
}
}
//Raycast
function Hover(){
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 20));
if (Physics.Raycast (ray, hit, distance)){
hit.transform.SendMessage("Hovers", Hovering, SendMessageOptions.DontRequireReceiver);
}
}
Comment
Answer by tanoshimi · Apr 30, 2015 at 07:07 PM
You're setting showLabel
to false in every frame on line 7 of Interactee.js.
When I hover over the object with that script it changes to true according to the console.
Edit: Never $$anonymous$$d I see the print was before the Hover.
How do I get showLabel to go back to false after I stop hovering?