- Home /
OnTriggerEnter not working properly.
Hello. The problem: Whenever I walk into the Trigger and press space, it does what I want. However, if I go back outside the collider, I can still activate it by pressing space. This is not ideal. I want it to only activiate when I am inside the trigger. Thank you.
Script (C#):
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class TheFinale : MonoBehaviour {
public string Footsteps;
public string MouseLook;
public string PlayerMovement;
public GameObject MainCamera;
public GameObject FirstPersonPlayer;
public Animator thefinale;
public Animator animat;
public Animator notasbad;
public Animator therose;
public Animator camanim;
public Animator rainanim;
public Text textthepromise;
public Text messenger;
public RawImage black;
public AudioSource dirtsteps;
public bool InTrigger;
Collider Otha;
// Use this for initialization
void Update()
{
if (InTrigger)
{
if (Otha.gameObject.name == "Shovel")
{
thefinale.SetBool("ThePromiseIN", true);
if (Input.GetKeyDown("space"))
{
(FirstPersonPlayer.GetComponent(Footsteps) as MonoBehaviour).enabled = false;
(FirstPersonPlayer.GetComponent(PlayerMovement) as MonoBehaviour).enabled = false;
(MainCamera.GetComponent(MouseLook) as MonoBehaviour).enabled = false;
therose.SetBool("rosepara", true);
camanim.SetBool("cameraparaAnim", true);
StartCoroutine(Final());
StartCoroutine(Notasbadco());
dirtsteps.Stop();
}
}
}
}
private void OnTriggerEnter(Collider other)
{
Otha = other;
InTrigger = true;
}
private void OnTriggerExit(Collider other)
{
InTrigger = false;
}
IEnumerator Final()
{
yield return new WaitForSeconds(5);
rainanim.SetBool("rainboolOut", true);
animat.SetBool("BlackFadeParaOUT", true);
yield return new WaitUntil(() => textthepromise.color.a == 0 && black.color.a == 1);
yield return new WaitForSeconds(8);
SceneManager.LoadScene(2);
}
IEnumerator Notasbadco()
{
yield return new WaitUntil(() => black.color.a == 1);
notasbad.SetBool("notbadparaIN", true);
}
}
As far as I can see, nothing in your update method should run while your bool 'InTrigger' is false.
Have you tried putting Debug.Logs into your OnTriggerEnter and OnTriggerExit methods to confirm that they're being called when you enter and leave the trigger?
Have you tried putting Debug.Log(InTrigger) at the top of your Update method so that you can see it changing as you move in and out of the trigger?
Yes, when I implement a Debug Log in Void Update saying TriggerTest, it continuously activates. When I put individual Logs in both TriggerEnter and Trigger Exit, both of them get called once. Also, sorry for the lack of information in the question, so please ask if you need more. I had 2 minutes to write before something important came up.
Answer by Whitewalkergp · Mar 07 at 06:02 PM
Hello Maybe Something Else Enters The Trigger and it still triggers your InTrigger boolian. Try giving your player the Tag "Player" and use this instead:
private void OnTriggerEnter(Collider other)
{
Otha = other;
if (other.CompareTag("Player"))
InTrigger = true;
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
InTrigger = false;
}
Yes, you were right! Turns out I had a separate script that interfered with the trigger upon pressing space that would re-enable it. ( don't question how I managed that... ) Anyways, it's fixed now. Thanks a lot
Your answer
![](https://koobas.hobune.stream/wayback/20220613053912im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Deactivate/Activate FPS Movement 0 Answers
OntriggerEnter / Stay with the same Gameobject tags 0 Answers
OnTriggerEnter() and OnTriggerExit() called multiple times despite checks. 3 Answers
OnTriggerEnter function in c# 0 Answers
Problem with OnTriggerExit 1 Answer