- Home /
Disabling and enabling again colliders
Hey guy, i was wandering if some one could help me to solve this problem i'm having when try to disable the BoxCollider2D the player (green marked car in img) has and after 1 second, enable it again. This is the script the player has attached:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using Spine.Unity;
public class CarControllerBis : MonoBehaviour
{
public Animator animator;
public KeyCode teclas;
public Rigidbody2D rigidbody;
public GameObject GameOverCanvas;
public GameObject HudCanvas;
public GameObject Gas;
public float startTime;
public BoxCollider2D collider;
void Start()
{
GameOverCanvas.SetActive (false);
HudCanvas.SetActive (false);
animator = GetComponent<Animator> ();
rigidbody = GetComponent<Rigidbody2D> ();
collider = GetComponent<BoxCollider2D>();
}
void Update()
{
if (Input.GetKeyDown (teclas))
{
animator.SetTrigger ("Boost");
GetComponent<AudioSource>().Play ();
GetComponent<AudioSource>().pitch = Random.Range(0.8f, 1.2f);
HudCanvas.SetActive (true);
collider.enabled = false;
rigidbody.isKinematic = true;
}
}
void OnCollisionEnter2D (Collision2D collision)
{
if (collision.gameObject.tag == "Hazard")
{
HudCanvas.GetComponent<Animator>().SetTrigger("HudOut");
SoundManagerScript.PlaySound ("Hit");
animator.SetTrigger ("Hit");
Destroy(this.gameObject.GetComponent<Rigidbody2D>());
Destroy(this.gameObject.GetComponent<CarControllerBis>());
GameOverCanvas.SetActive (true);
DestroyObject (Gas);
}
}
}
[1]: https://pbs.twimg.com/media/D_Dr7sQXkAAqHWe.jpg:large
Answer by dbchest · Jul 11, 2019 at 01:06 AM
will you provide your question with descriptions of current and desired behavior for this script?
Sorry, my bad :) There you go!
This is the img i reffer: https://pbs.twimg.com/media/D_Dr7sQXkAAqHWe.jpg:large
Answer by I_Am_Err00r · Jul 11, 2019 at 01:21 AM
I already answered this in the post you had about this earlier, but I will just copy and paste real quick.
Add
public float timeTillEnabled;
to your list of variables at the top of your script, copy this part of the script you already have:
void Update()
{
if (Input.GetKeyDown (teclas))
{
animator.SetTrigger ("Boost");
GetComponent<AudioSource>().Play ();
GetComponent<AudioSource>().pitch = Random.Range(0.8f, 1.2f);
HudCanvas.SetActive (true);
collider.enabled = false;
StartCoroutine(TurnOnCollider());
rigidbody.isKinematic = true;
}
}
Then add this block outside of any function; I typically put these after all other voids:
IEnumerator TurnOnCollider()
{
yield return new WaitForSeconds(timeTillEnabled);
collider.enabled = true;
yield break;
}
Set the public float timeTillEnabled to 1 in the inspector, and it will come back on in 1 second; alternitively you could hard code it in the script:
yield return new WaitForSeconds(1f);
but setting it publicly in the inspector lets you change it later much easier
Your answer
![](https://koobas.hobune.stream/wayback/20220612204705im_/https://answers.unity.com/themes/thub/images/avi.jpg)