- Home /
How do i merge these things?
Hey, everyone! i'm working on a talking system, for a tutorial level. The aim is to activate the GUI elements in the script when you touch a cube with is trigger on. the script however right now, consists of two parts that need to be one: the part where the trigger outputs an action, and the part with the actual GUI:
public var text = "box text here";
public var NPCname = "NPC name here";
function OnTriggerEnter (other : Collider) {
print ("Test");
}
function Start () {
}
function OnGUI() {
GUI.Box(Rect(Screen.width / 6,Screen.height / 1.3,Screen.width / 1.5,Screen.height / 4),"Test 2");
}
Now, the first part makes the log print Test, that works fine, so we've got a working trigger. the second part is the setup for the GUI itself. my question is, what string of code allows me to have the trigger activate the GUI?
Answer by AlucardJay · May 21, 2014 at 08:38 PM
I strongly advice you do some tutorials to get the basics of programming, and learn what kinds of commands and variables are available to use.
Use a boolean that changes state based on if an object is in the trigger zone
var inTriggerZone : boolean = false;
function OnTriggerEnter (other : Collider) {
inTriggerZone = true;
}
function OnTriggerExit (other : Collider) {
inTriggerZone = false;
}
function OnGUI() {
if ( inTriggerZone ) // this is the same as writing if(inTriggerZone==true)
{
GUI.Box(Rect(Screen.width / 6,Screen.height / 1.3,Screen.width / 1.5,Screen.height / 4),"Test 2");
}
}
Here is a list of tutorials to get you going :
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/
Unity Learn : http://unity3d.com/learn
the Unity Wiki : http://wiki.unity3d.com/index.php/Tutorials
A list of resources : http://answers.unity3d.com/questions/12321/how-can-i-start-learning-unity-fast-list-of-tutori.html
Thanks, i am following tutorials, and i tried to write up some code from scratch myself as an exercise, but i couldn't find a tutorial for this specific thing... are you ok with it if i use this code?
Yes of course, this was an example written for you.
The next thing to check would be what entered the trigger. Check an objects name :
function OnTriggerEnter (other : Collider) {
Debug.Log( other.gameObject.name + " has just entered the trigger" );
if ( other.gameObject.name == "Player" ) // did an object named Player enter the trigger?
{
inTriggerZone = true;
}
}
you can use tags :
if ( other.gameObject.tag == "Player" )
It all starts from here. Happy Coding =]
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Why doesn't my GUI.Label show up? 1 Answer
Is it possible to link character skill lists to a GUI, and if so, how? 3 Answers
JavaScript: GUI Box help 1 Answer
Pause Menu background problem 0 Answers