- Home /
Enable/ Activate a script by script
Let's say that there are 2 objects: Activator and Gate.
Activator has a script called ["ActivatePortal"]. Gate has a script called ["Portal"].
I need Activator script to activate Gate script. So if Activator script ["ActivatePortal"] is not somehow activated, you cannot activate Gate script ["Portal"].
ActivatePortal script:
var script: LoadNextLevel;
function OnTriggerStay (){
if (Input.GetKeyDown(KeyCode.JoystickButton1)){
script = GetComponent(Portal);
script.enabled = true;
Debug.Log("Sucessfully activated!");
}
}
Portal script:
function OnTriggerEnter (myTrigger : Collider) {
if(myTrigger.gameObject.name == "Player"){
Debug.Log("Player went through! Level2 loaded.");
Application.LoadLevel("Level2");
}
}
GetComponent() is the typical way to communicate between two components.
http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
http://unitygems.com/script-interaction-tutorial-getcomponent-unityscript/
Answer by MagicoCreator · Jul 13, 2014 at 11:20 PM
function OnTriggerEnter (myTrigger : Collider) {
if(enabled){
if(myTrigger.gameObject.name == "Player"){
Debug.Log("Player went through! Level2 loaded.");
Application.LoadLevel("Level2");
}
}
}
Use it like this and it will work like you wanted it to, because OnTriggerEnter is still sent regardless if the script is enabled or not.
Setting enabled to false only disables the automatic calling of Update,LateUpdate, and FixedUpdate, as far as i know
I get this...
NullReferenceException: Object reference not set to an instance of an object ActivatePortal.OnTriggerStay () (at Assets/Scripts/ActivatePortal.js:5)
GetComponent only works on the GameObject, so if your script runs on another gameobject you should get the portal script like this: GameObject.Find("nameoftheportalobject").GetComponent(Portal);
Your answer
![](https://koobas.hobune.stream/wayback/20220613153943im_/https://answers.unity.com/themes/thub/images/avi.jpg)