Creating re-usable action triggers

Hi guys,

I’m a unity & programming noob, just starting to wrap my head around certain coding concepts.
The demo I’m working on has a variety of actions the player can do, by pushing a button while in proximity of an object. The flow might go like this:

Player walks in to proximity trigger > Action button is displayed > Player pushes button > Something happens

The “something happens” can be different things: opening a door, having a message window displayed, destroying an object. But they are all initiated in the same manner.

I understand how to set up the individual actions, but not how to make the code more reusable. For example, I can make a “door open” scipt that contains the trigger/button/action code. But if I then make a “display message window” script, it doesn’t seem efficient to rewrite the same trigger/button code inside that as well. Or does it?? :open_mouth:

This leads me to believe I should somehow bundle the first actions (trigger/button display/button push) in to a re-usable piece of code, that I could apply to any object. And then somehow change the “something happens” part, depending on what the object is.

So my question is, what’s the best way to go about doing this?

  1. Making the trigger/button code re-usable - so I can put it on any object I want to be interactive

  2. Implementing the “Something happens” part? If I make a simple “open door” movement script, how can I activate this after my generic trigger action above?

Thanks for any advice!
(I’m using javascript btw, if that makes any difference).


Here’s an example of a script that I have on a trigger.
It displays a button icon when you enter the trigger, then displays a message window once you push the button.

What I would like to do is take something like this, and be able to use it on different objects - so that I could trigger different messages, or different actions, once the player pushes the context action button.


var showContextButton : boolean = false;
var showQuest : boolean = false;
var buttonImage : Texture;
var buttonAudio : AudioClip;
var buttonPushAudio : AudioClip;
var gamePlayer : GameObject; //Player Object
var gameCamera : GameObject; //Main Camera

//when player enters trigger radius, play a sound and show context button
function OnTriggerEnter (myTrigger : Collider) {
	if(myTrigger.gameObject.name == "Player"){
		AudioSource.PlayClipAtPoint(buttonAudio, transform.position);
		showContextButton = true;
	}
}

//stop showing context button when player leaves the trigger
function OnTriggerExit (myTrigger : Collider) {
	if (myTrigger.gameObject.name == "Player") {showContextButton = false;}
}

//show context button or quest window
function OnGUI () {
	if (showContextButton){
		GUI.DrawTexture(Rect((Screen.width / 2),(Screen.height * .75),64,64),buttonImage);
	}
	if (showQuest){
		FreezeOn();
		windowRect = Rect (10,10,300,150);
		windowRect = GUI.Window (0, windowRect, DoMyWindow, "Quest");
	}
}

//if player pushes context button, show quest window
function OnTriggerStay (myTrigger : Collider) {
	if(Input.GetKeyDown(KeyCode.E)){
		AudioSource.PlayClipAtPoint(buttonPushAudio, transform.position);
		Debug.Log("Action!");
		showContextButton = false;
		showQuest = true;
	}
}

//contents of the quest window
function DoMyWindow (windowID : int) {
	GUI.Label(Rect(10,25,280,100), "Your quest is to go to the desk!");
	if (GUI.Button (Rect (10,115,280,25), "Okay")){
		FreezeOff();
		showQuest = false;
		setPrefQuestDesk();
	}
	else{showQuest = true;}
}

//disable movement
function FreezeOn(){
	gamePlayer.GetComponent(MouseLook).enabled = false;
	gamePlayer.GetComponent(CharacterMotor).enabled = false;
	gameCamera.GetComponent(MouseLook).enabled = false;
	Screen.showCursor = true;
}

//enable movement
function FreezeOff(){
	gamePlayer.GetComponent(MouseLook).enabled = true;
	gamePlayer.GetComponent(CharacterMotor).enabled = true;
	gameCamera.GetComponent(MouseLook).enabled = true;
	Screen.showCursor = false;
}

//set quest to active
function setPrefQuestDesk(){
	PlayerPrefs.SetString("QuestDesk", "active");

}

I think the best choice will be having a function on the object affected maybe called InUse() and make them have a tag of “Usable”

then in the player code may not work doing it by head.

OnTriggerStay(other : Collider) {
if(other.tag == “Usable”) {
if (Input.GetButton(“use”)) {
sendMessage(other, “InUse”);
}