- Home /
Script won't reactivate after I deactivate it through a script
Hey all. I've got this script that is supposed to deactivate motion of the stock First Person Controller, and set a bool variable in a different script to true. Here it is:
using UnityEngine;
using System.Collections;
public class LadderAcend : MonoBehaviour
{
public GameObject player;
private Climb climb;
void Awake ()
{
player = GameObject.FindGameObjectWithTag("Player");
climb = player.GetComponent<Climb>();
}
void OnTriggerEnter (Collider other)
{
if(other.tag == "Player")
{
player.GetComponent<FPSInputController>().enabled = false;
player.GetComponent<CharacterMotor>().enabled = false;
climb.canControl = true;
}
}
void OnTriggerExit (Collider other)
{
if(other.tag =="Player")
{
player.GetComponent<FPSInputController>().enabled = true;
player.GetComponent<CharacterMotor>().enabled = true;
climb.canControl = false;
}
}
}
I have a custom movement script that is activated by the canControl variable being set to true.
When I enter the trigger, the Character Motor and FPSInputController are disabled, and canControl is true, but when I exit the collider, the scripts are not re-enabled, neither is canControl set to false. Any ideas?
@caleb_b , are you sure that your player is in fact exiting the trigger? Set a debug statement in OnTriggerEnter and OnTriggerExit to make sure. Are you perhaps disabling the collider when you disable those components?
I put Debug.Log("Entered");
in OnTriggerEnter and Debug.Log("Exited");
in OnTriggerExit. "Entered" pops up, but only once. "Exited" never appears, even though I am far outside of the boundary of the collider, as shown here (the green box in the scene view is the collider, and the capsule is the player)
I don't think the collider is changing at all, because I watch the colliders setting while in Play mode, and it is neither deactivated, nor set to "not trigger"
What happens if you use this?
void OnCollisionExit(Collision other)
{
if(other.collider.tag =="Player")
{
print("Somethings going on");
player.GetComponent<FPSInputController>().enabled = true;
player.GetComponent<Character$$anonymous$$otor>().enabled = true;
climb.canControl = false;
}
}
Replacing my OnTriggerExit code with yours changes nothing. Changing my OnTriggerEnter to OnColliderEnter and making appropriate changes(True to false, visa versa), nothing happens when I enter the trigger.
Well, the problem is most likely your player object. Do you actually have at least a kinematic rigidbody attached? Without one you don't get collision or trigger messages. Next thing is how does your "custom movement" script move your player? You might somehow bypass the physics system. You should add some more details about your playerobject.
Answer by ABerlemont · Jun 24, 2014 at 09:07 AM
After a script is deactivated through .enabled it cannot be found by a GetComponent. You need to put that reference in a variable.
GetComponent is process heavy, you should always call it at the start of the game and keep reference of what you need to interact with in variables.
Handy bit of info, and that seems like a more efficient way to handle references like those, but that doesn't do anything. OnTriggerExit is still not being called, unless I take out the deactivation codes.
So your problem is that OnTriggerExit is never called ? That's not clear in your post. Is LadderAcent disabled elsewhere ?
Your answer
Follow this Question
Related Questions
How do I disable all rigidbodies and colliders in a scene in unity? 2 Answers
Load a GameObject that is outside of script and set it active at the same time 1 Answer
Enable and disabled parent contraint with c# 1 Answer
c# How Do I disable current script or anyother script in the program within code? 2 Answers
using Aim Constraint 1 Answer