- Home /
Activate a GUI Text on Collision?
hello
I need to be able to get some GUIText to display when my Player collides with a floor panel (called "Warehouse Floor End" and only object in scene with that name). It doesn't matter if the text is in a button/box as of code or a GUIText GameObject (have one in scene called "Well Done Message" and again is only object in scene with tha name.
P.S. It doesn't matter what format the script is in (Java or C#)
Answer by Iceblitzyt · Oct 21, 2012 at 12:31 AM
You can use this script:
using UnityEngine;
using System.Collections;
public class messagebox : MonoBehaviour {
public Transform other;
void OnGUI() {
if (other) {
float dist = Vector3.Distance(other.position, transform.position);
if(dist < 10 ){GUI.Box (new Rect(0,0,100,100),"Well done!!");
}
}
}
}
This is a c#script, add this to the floor panel and set the dist to what ever eg <5 <1 ect. Once you attached the script to the floor panel, in the inspector click the other and select your player (your character controller, ie first person controller ect)
I've tried to use it but the GUI box doesn't display on the HUD of the player
Answer by hvilela · Oct 20, 2012 at 10:05 PM
void OnCollisionEnter(Collision collision) {
if (collision.name = "Warehouse Floor End") {
guiText.enabled = true;
}
}
is it supposed to be
if(collision.name == "Warehouse Floor End")
?
can you use
if(collision.tag == "Warehouse")
and would it have the same effect? (provided the gameObject is tagged)