- Home /
Question by
jolly_joll · Dec 16, 2012 at 11:35 PM ·
collisionyield
How to toggle gameObject collision
I have a Gameobject with collision that I'm attempting to turn on and off on input of touch/mouse button.
I would like there to be a delay before the collision is re-enabled.
Ideally I don't want to destroy the game object just toggle its collision.
By no means am I a coder but trying to learn and attempted to give it a go. Unfortunately it doesn't seem to be working but I think I'm close! Could you please have a look at my script to see where I have gone wrong.
Many thanks!
#pragma strict
function Start (){
}
function Update (){
collider.enabled = true;}
if(Input.GetMouseButtonUp(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended))
{
collider.enabled = false;
Wait();
}
function Wait (){
yield WaitForSeconds (2);
}
Comment
Best Answer
Answer by clunk47 · Dec 17, 2012 at 02:06 AM
#pragma strict
function Update ()
{
print(collider.enabled);
if(Input.GetMouseButtonUp(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended))
{
collider.enabled = false;
Wait();
}
}
function Wait()
{
yield WaitForSeconds (2);
collider.enabled = true;
}
Answer by jolly_joll · Dec 17, 2012 at 11:42 AM
Thank you for your help. That has worked!
I just tried to use your code but its not working for me. can you tell me what I did wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ToggleTang : $$anonymous$$onoBehaviour {
void Update () {
print(collider.enabled);
if (Input.Get$$anonymous$$ouseButtonUp(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended))
{
collider.enabled = false;
Wait();
}
}
function Wait()
{
yield WaitForSeconds (2);
collider.enabled = true;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613085528im_/https://answers.unity.com/themes/thub/images/avi.jpg)