- Home /
Moving GUIbutton over an object that does not follow the player?
I would like to have a GUIbutton that stays over an object and does not move when the player moves, is this possible? I'm not sure exactly how to do that, as I have only made GUIbuttons that stay on screen when the player moves. Here is my current code:
using UnityEngine;
using System.Collections;
public class DialoguerExampleStart : MonoBehaviour {
private bool bShowGUI = false;
void Awake(){
// You must initialize Dialoguer before using it!
Dialoguer.Initialize();
}
void OnTriggerEnter2D (Collider2D other){
if (other.tag == "Player") {
bShowGUI = true;
}
}
void OnTriggerExit2D (Collider2D other) {
if (other.tag == "Player") {
bShowGUI = false;
}
}
void OnGUI(){
if (bShowGUI) {
if (GUI.Button (new Rect (640, 290, 100, 30), "Investigate")) {
Dialoguer.StartDialogue (0);
}
}
Answer by yashpal · Nov 29, 2014 at 06:12 AM
@OctoSloths, I personally think It's not possible. OnGUI (GUI.Button) is made on camera. so it's not stay with your character. For that you want to make button form sprite. and put it as child of your character so when character move it also move with your character. and use OnMouseDown to detect click.
Here is onMouseDownTutorial.
Answer by Kiwasi · Nov 29, 2014 at 07:11 AM
Consider upgrading to 4.6 and creating a world space canvas as a child of your moving GameObject.