- Home /
How to enable or disable a script OnTriggerEnter?
Ok, so I have been searching all over for help and right when I think I find a solution, I encounter a problem. I haven't scripted in a bit, so I am a bit rusty. Anyways, I want to enable a script of a different GameObject (in this case, one on the Main Camera in the FPS player prefab) and I can't seem to get it. Any time I try to use ".enabled" I get a syntax error. I have done this before, but cannot remember how. Here is what I have: #pragma strict
function Start ()
{
var Blur : GameObject.Find("First Person Controller");
Blur.GetComponent("Indie Effects").enabled = false;
}
function OnTriggerEnter (col : Collider)
{
if (col.tag == "Player")
{
Blur.active = true;
}
}
function OnTriggerExit ()
{
Blur.active = true;
}
Answer by Tony_T · Apr 25, 2014 at 09:49 AM
Try this:
var Blur: GameObject; //The game object that has the script
Blur.GetComponent.<ScriptName>().enabled = false;
Nope. Didn't work. Now the error gotten is BCE0019: 'IndieEffects' is not a member of 'function(System.Type): UnityEngine.Component'. Here is the script as updated: #pragma strict
var Blur : GameObject;
function Start ()
{
Blur.GetComponent.IndieEffects().enabled = false;
}
function OnTriggerEnter (col : Collider)
{
if (col.tag == "Player")
{
Blur.GetComponent.IndieEffects().enabled = true;
}
}
function OnTriggerExit ()
{
Blur.GetComponent.IndieEffects().enabled = false;
}
You missed the ""... it should look like this:
Blur.GetComponent.<IndieEffects>().enabled = false;
If it still doesn't work, place the indie effect script in the standard assets but only if it's in C#.
Ok. Ill try that.
Edit: Nope. It works, no compiler errors, but now the error is "NullReferenceException: Object reference not set to an instance of an object Underwater.Start () (at Assets/Scripts/Underwater.js:7)" Here is the updated script with out the compiler errors:
#pragma strict
var Blur : GameObject;
function Start ()
{
Blur.GetComponent.<IndieEffects>().enabled = false;
}
function OnTriggerEnter (col : Collider)
{
if (col.tag == "Player")
{
Blur.GetComponent.<IndieEffects>().enabled = true;
}
}
function OnTriggerExit ()
{
Blur.GetComponent.<IndieEffects>().enabled = false;
}
In the inspector look at the gameobject this script is attached to, there will be a field called Blur - have you drag/dropped the target gameobject into that?
Your answer
