Hello, Everyone this is my first post on the unity forums and I need help.
I have looked everywhere for this script, but either find it not matching my
criteria or I find it difficult to edit due to my lack of knowledge in the area
of JavaScript (unity based)
Here is the script I have found and edited, so far.
var Soundfile : AudioClip;
function OnTriggerEnter(){
if (Input.GetKeyDown(“e”)
)
audio.PlayOneShot(Soundfile);
}
@script RequireComponent(AudioSource)
But what I want is for my character (a first person controller) to enter a collision box (with trigger checked) and be allowed the option to press “E” once in that area. I have put together a basic idea in Photoshop.
It would be a massive help, if somebody knew how to add a GUI text, once the player also entered that area.
(you may know of Pablo Francisco, its a place holder)
(The whole idea for this project of mine is for fun, it is an economy game, were you can get a job on a space-station, buy apartments, work at a mundane undermining job (fifth element style) etc.
Also, not to be a pain is there any sites anybody knows to get me kick started in JavaScript (hopefully with videos) .
Thanks Everyone, I love you all. 
try it like this
@script RequireComponent(AudioSource)
var Soundfile : AudioClip;
private var isTriggered: boolean = false;
function OnTriggerEnter (other : Collider) {
isTriggered = true;
}
function OnTriggerExit (other : Collider) {
isTriggered = false;
}
function OnGUI() {
if (isTriggered) {
GUI.Label(Rect(10,10,100,20), "Player in the area!");
}
}
function Update() {
if (Input.GetKeyDown(KeyCode.E) isTriggered) {
audio.PlayOneShot(Soundfile);
}
}
you can have a bool value and set that default to false. and when you enter the area (OnTriggerEnter) set that bool to true
and in the update function you can check for it.
quick example in C#:
public bool triggersound = false;
void Update(){
if(triggersound)
if (Input.GetKeyDown("e"))
audio.PlayOneShot(Soundfile);
}
void OnTriggerEnter(){
triggersound = true;
}
void OnTriggerExit(){
triggersound = false;
}
Edit: oh someone beat me to it, and in javascript too perfect. ignore mine lol
@GrandMasterHsu
your code remainded me that i forgot OnTriggerExit()
that’s fixed now.
ah thanks everyone, worked like a charm. I am now trying to change the font of the GUI through script on the same script, but it shouldn’t be to hard. I love how easy your scripts were to understand as well, thank you again.