How to Deactivate a Trigger collider on a Specific GameObject
Hello everyone, I've been working on a Systematic Dialogue that Plays out without the player pressing any buttons (meaning it automatically plays when he collides in another NPC) but what I find Happening is that the Player can quite easily spam the NPC dialogue which I don't want.
So I introduced a new Coroutine called WaitBeforeReplay()
but and place the Start Coroutine in the OnTriggerExit
function, but it doesn't work. How can I go about this?:
using System.Collections;
using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class GreetKojo : MonoBehaviour { private Animator anim; public GameObject NPC;// So we can use this script on multiple NPC's instead of doing multiple scripts public GameObject Dialogue; public GameObject KojoDialogue;// Dialog for Kojo public float waitTime;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
}
//When collision is made, trigger the greeting animation and show pop up text
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
anim.SetBool("Notice", true);//Start animation loop in bool
Dialogue.SetActive(true);//Hide pop up text and disxontinue greeting animation
}
}
//Whenever the Player, exits the trigger resume regular idle loop
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
anim.SetBool("Notice", false);//Stop animation looping bool
Dialogue.SetActive(false);// Deactivates the canvas after Player leaves collision
StartCoroutine(KojoRes());
}
}
//Deactivates KoJo's Dialogue
private IEnumerator KojoOff()
{
yield return new WaitForSeconds(waitTime);
StartCoroutine(WaitBeforeReplay());
KojoDialogue.SetActive(false);
}
//Activates KoJo's Dialogue
IEnumerator KojoRes()
{
yield return new WaitForSeconds(2);//Wait for x amount of seconds
KojoDialogue.SetActive(true);//Then set Kojo's dialogue on
StartCoroutine(KojoOff());//After which we wait x amount to deactivate the dialogue
}
private IEnumerator WaitBeforeReplay()
{
//Wait for x amount of time before allowing dialogue to appear again
yield return new WaitForSeconds(6);
GetComponent<Collider2D>().enabled = false;
}
} [/CODE]
Answer by tormentoarmagedoom · May 08, 2019 at 04:28 PM
Hello.
I did not read the code, jsut answering your question.
You need to find the object. Once you have the object, reach the compoenent, then use enabled=false
TheObject.GetComponent<BoxCollider>().enabled=false;
Bye!
But you can't answer the question like that..... Because for the most part I have that already included, I just don't know if I did it right n needed help in troubleshooting to see what's actually working or not. Thanks for the assistance, but it didn't help me in this case, so I think it's my coroutines at fault
I did some debugging and added the syntax you gave, it worked. Thank you for your help once again. The issue was really in how I structured the coroutine syntax and that lil part that you gave me. :)
Answer by DaresFireson · May 08, 2019 at 04:50 PM
try this
gameObject.GetComponent<Collider2D>().istrigger = false;