So I have a single c# script, which I’ve attached to multiple game objects. I want to get the collider attached to the game object that the script is attached to, but the problem is that when I try to do so, it returns the collider of only 1 of the objects the script is attached to, and it’s always the last one I place in the scene.
I first declare I want a collider called myCollider -
Collider myCollider;
Then in void Start () I try to assign the collider, and I assume this is where I’m going wrong, but I’ve done this so many times I don’t see how I can have got it wrong.
void Start () {
myCollider = gameObject.GetComponent<Collider>();
}
I want to then disable the collider during OnMouseDown
void OnMouseDown () {
myCollider.enabled = false;
}
Finally I try and enable the collider in void Update () using the following code.
void Update () {
if (Input.GetMouseButton(1)){
myCollider.enabled = true;
}
}
The problem is no matter which game object this is attached to, when I right click, the collider that gets enabled is on one single object, not the one the script is attached to, and it’s the last one I put in the scene.
I normally wouldn’t ask a question, but I’ve searched for hours for an answer, and tried a LOT of other things. Making myCollider private doesn’t work, and I don’t know what else it can be. Please help.