Augmented Jacob
Augmented Jacob

Reputation: 1577

How to Selectively Disable Sphere Colliders?

I'm trying to build a pool game where the pot has a collider. Now on collision with a ball, I expect that particular ball that collided to disable.

The code below is what I've tried but it only allows me to set the ball manually. How do I automat and detect the right ball directly?

using UnityEngine;


using System.Collections;    
public class pot: MonoBehaviour
{   //allows me to set collider
    public SphereCollider ball;

    void OnTriggerEnter(Collider other) {
        Debug.Log("Ball potted");
        ball = GetComponent<SphereCollider>();
        ball.enabled = !ball.enabled;
        }

}

Upvotes: 1

Views: 1303

Answers (3)

Mark
Mark

Reputation: 1

public SphereCollider sphereCollider;

void Update()
{
    if (Input.GetKeyDown(KeyCode.Q)) //or however you want to call it.
    {
        sphereCollider = gameObject.GetComponent<SphereCollider>();
        sphereCollider.enabled = false;
    }
}

Upvotes: 0

yoyo
yoyo

Reputation: 8726

It looks like your code is getting the sphere collider of the pot itself. Isn't "other" the ball in this case?

You can disable the "other" object (assuming it is the ball) with "other.gameObject.SetActive(false)" -- that's if you want the whole ball to disappear. If you just want it's collider to stop working then use "other.enabled = false".

Upvotes: 2

PeterC
PeterC

Reputation: 126

Is

other.enabled = false;

what you are looking for?

Upvotes: 2

Related Questions