- Home /
How to stop a script in c#
i want that after the closet goes forward the script ends so that when you press e again the drawer won't move, ho can i do this, the sctip s this one:
using UnityEngine; using System.Collections;
public class Drawer : MonoBehaviour {
public bool inTrigger = false;
public GameObject Closet;
void OnTriggerEnter (Collider Other){
if(Other.tag == "Player"){
inTrigger = true;
}
}
void OnTriggerExit(Collider Other){
if(Other.tag == "Player"){
inTrigger = false;
}
}
void OnGUI (){
if(inTrigger == true){
GUI.Box(new Rect (500,300,150,25),"Press E to open");
}
}
void Update (){
if(inTrigger == true){
DrawerMovement();
}
}
void DrawerMovement(){
if(Input.GetKeyDown(KeyCode.E)){
transform.position = Closet.transform.position - Vector3.left* 1f;
}
}
}
use a boolean, such as. this is highly simple, just to show you how it can be made, and you should be checking this boolean in other parts of the script, where necessary.
bool drawerOpen = false;
void Drawer$$anonymous$$ovement(){
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E) && !drawerOpen){
transform.position = Closet.transform.position - Vector3.left* 1f;
drawerOpen = true;
}
}
If you never want to execute this script on this object again, you can Destroy it:
Destroy(this);
If you just want to turn it off for awhile, then @koray1396's use of a boolean is the way to go.
Answer by sillyjake · Nov 27, 2014 at 11:29 PM
public bool scriptActive;
if (scriptActive) {
}
Put this in all your functions, And you can have a bool toggle it on and off.
Answer by Tappo · Nov 28, 2014 at 12:40 AM
thanks, but i fixed in a easyer way, by deleting the collider after the end of the script, but thanks to all of u