- Home /
Disable Collider After two Second of Being Triggered and enable it again
Hi guys i need help making a collider disabled for two seconds when collided with the player and reenable it after 2 second.Everytime my Player collides with the collider it adds 1 score so i need a script to disable it for 2 seconds when collided with the player so that the player cant collide 2 times and get double score and then reenable again.Here's my current script but i keep getting an error.Thanks
using UnityEngine;
using System.Collections;
public class ScorePoint : MonoBehaviour {
void OnTriggerEnter(Collider collider) {
if(collider.tag == "Player")
Score.AddPoint(
(Collider = false)
(WaitForSeconds (2)
(Collider = true)
}
}
Since your collider seems to be a trigger you could simply disable your collider component when the player hit the collider and then reenable it in a Coroutine.
I am sorry i am a newbie.How would i disable my collider component when the player hit the collider and then reenable it in a Coroutine. I dont really understand that but is this it
void OnTriggerEnter(Collider collider) {
if(collider.tag == "Player")
Score.AddPoint(
How do i coroutine it? Thanks
Answer by Albert-han · Jun 10, 2014 at 01:36 AM
Thanks guys i finally have a working script awesome! Heres the final code and i think figured out where to put ; and ) stuff using UnityEngine; using System.Collections;
public class Score2 : MonoBehaviour {
IEnumerator OnTriggerEnter(Collider collider) {
if(collider.tag == "Player") {
Score.AddPoint();
gameObject.collider.enabled = false;
yield return new WaitForSeconds (2);
gameObject.collider.enabled = true;
}
}
}
Answer by Kiwasi · Jun 10, 2014 at 12:23 AM
You have so many coding errors here. Try this for starters and you may start getting sensible errors. (C# code)
using UnityEngine;
using System.Collections;
public class ScorePoint : MonoBehaviour {
IEEnumerator OnTriggerEnter(Collider collider) {
if(collider.tag == "Player") {
Score.AddPoint();
collider.enabled = false;
yield return new WaitForSeconds (2);
collider.enabled = true;
}
}
}
Remember the basics. Every line should end in a ; or {. Every ( needs a ). Every { needs a }.
Parsing error basically means the compiler has no clue what you are talking about.
I got the ienumerator working but now i get flooded with error messages i dont understand and i stops my character from moving when it colides with the collider. Error message is CharacterController.$$anonymous$$ove called on inactive controller UnityEngine.CharacterController:$$anonymous$$ove(Vector 3) and i notice my character controller also disables when i touch the collider.
I think it has something to do with my character controller script movement.collisionFlags = controller.$$anonymous$$ove (current$$anonymous$$ovementOffset); because im using a 3d character controller for my 2d game with forward and backward disabled.
So now we are disabling the collider on your player ins$$anonymous$$d of the ScorePoint
Change line 9 and 11 to
gameObject.collider.enabled = ...
If it works I will update my answer
Answer by screenname_taken · Jun 09, 2014 at 02:32 PM
If this is a direct copy paste from your script, you forgot to add a couple of ")"
exactly where am i missing ")"? because i am new and i dont know where to put those things
Just start pairing them. each ( needs to have a ) and each { needs to have its }. What is the error that unity gives you? It tells you the line and column that the error is at. You should really just give the unity manual a read. It has everything you need to get started in there.
Your answer
Follow this Question
Related Questions
Ragdoll help 1 Answer
How to disable collision detection while a UI Panel is enabled 2 Answers
How do I disable all rigidbodies and colliders in a scene in unity? 2 Answers
Why does Unity disable the Collider when I Destroy the Rigidbody? (2D) 1 Answer
How to change settings to a rigidbody when adding it to a gamobject? 2 Answers