- Home /
How to disable a script with OnTriggerEnter?
I am trying to disable two scripts I have on two different game objects after a collision enters a trigger. I am not sure how to set it up becasue I keep getting errors for everything I try. Thanks for any advice and your time. Here is my script:
#pragma strict
var cameraFollow : CameraFollow;
var planeSelfMove : PlaneSelfMove;
function OnTriggerEnter( other : Collider )
{
if(other.gameObject.tag == "CameraTrigger")
{
cameraFollow = gameObject.GetComponent(CameraFollow).enabled = false;
planeSelfMove = gameObject.GetComponent(PlaneSelfMove).enabled = false;
}
}
Edit: I have changed my script to this (below) thanks to sharpshot124 because well it makes sense but now I get this null reference error: NullReferenceException: Object reference not set to an instance of an object StopCamera.OnTriggerEnter (UnityEngine.Collider collider) line 13
Line 13 is the cameraFollow.enabled = false;
So I am going into detail about my problem(which I should have done first). Here is what I am trying to do:
I am making a top-down shooter and the camera is in the orthographic view, I have "camera collision" around the edges so the player can't go outside the camera's view. I am using the top(top of the screen) camera collision to act as the collision for triggers to do things like spawn enemies and other things. And that works totally fine. But I get all the way through my level to the area where the boss fight is and I want to stop the camera from moving (it has a script attached to it that makes it move at a certain speed constantly). I changed my script to the necessities and to what sharpshot124 suggested. The game plays but when the top camera collision enters that trigger nothing happens and I get that error. The top camera collision does have a rigidbody so thats fine I believe. Also I'm attaching the script to the top camera collider. The trigger I am trying to get to work is named "SpawntriggerStopCamera". Can anyone shed some light as to why I get that error? Again here is the revised script:
#pragma strict
var cameraFollow : CameraFollow;
function OnTriggerEnter( collider : Collider )
{
if(collider.gameObject.name == "SpawntriggerStopCamera")
{
cameraFollow = gameObject.GetComponent(CameraFollow);
cameraFollow.enabled = false;
}
}
What errors (post error message from the Console if possible), and on what line.
Answer by Kleptomaniac · Mar 09, 2013 at 11:07 PM
Nobody here is giving you a clear answer.
I would set out your script like so:
#pragma strict
var topdownCam : GameObject;
private var cameraFollow: CameraFollow;
private var planeSelfMove: PlaneSelfMove;
function Start () {
cameraFollow = topdownCam.GetComponent(CameraFollow);
planeSelfMove = GetComponent(PlaneSelfMove);
}
function OnTriggerEnter (collider : Collider) {
if (collider.game object.tag == "SpawntriggerStopCamera") {
cameraFollow.enabled = false;
planeSelfMove.enabled = false;
}
}
I declared the script component references so that it wouldn't slow your code each time when creating a new component reference. :)
Hope that helps,
Klep
EDIT:
Now all you have to do is assign your TopCameraCollision object to the topdownCam variable in the Inspector, and you're good to go! :)
Thanks for the reply but I still get the Null Reference Exception with the "cameraFollow.enabled = false;" line. I don't know if this information will make any difference but when I click on the error in the log, the "TopCameraCollision" Game Object gets highlighted.
Ah right, so your CameraFollow script is attached to your TopCameraCollision GameObject? O$$anonymous$$, I'll edit my script above to fix this.
Actually my cameraFollow is attacted to my camera. But I will try messing with the script fix and change it to the camera.
Answer by sharpshot124 · Mar 09, 2013 at 05:38 AM
lines 11 and 12 are invalid. u cannot have 2 statements on one line change it to this
cameraFollow = gameObject.GetComponent(CameraFollow);
cameraFollow.enabled = false;
planeSelfMove = gameObject.GetComponent(PlaneSelfMove);
planeSelfMove.enabled = false;
also make sure that one of the objects has a non trigger collider and one object has a rigidbody, otherwise the function wont be called at all.
For your suggestion i get this error: BCE0020: An instance of type 'UnityEngine.Behaviour' is required to access non static member 'enabled'.
Answer by 3dws · Mar 09, 2013 at 06:41 AM
hi script is a Component . u must access to your script component to disable it
u can access it in 2 way ;
1 : if script attached to external object u must use this code
gameobject.find("objectname").getcomponent(scriptname).enable=false;
2 : if u want to disable component that attached to object by itself , u can use this code
transform.getcomponent(scraiptname).enable=false;
With your suggestion i get this error: BCE0019: 'enable' is not a member of 'CameraFollow'. BCE0019: 'enable' is not a member of 'PlaneSelf$$anonymous$$ove'.