I am using several box colliders on the same GameObject, the question is, is it possible to refer to each of those colliders separately using
gameObject.getComponent().enabled = false;
Keep in mind that I have few colliders and I want some of them to stay enabled while the others will be disabled.
What I do is create empty child GameObjects [One GameObject for every collider] and assign colliders to them, and then I would assign tags to those children [in your case they can be 'AlwaysActive' and 'SwitchingActive' or whatever better name you can come up with].
Then in the parent I will use GetComponentsinChildren<Collider>
to find all the colliders, and check if the tag of the GameObject (of a respective collider) matches my requirement or not. If it does I will do my required task otherwise skip it.
NOTE:
Colliders on child GameObjects do not call OnTriggerEnter
or OnCollisionEnter
in parent script. I use delegate strategy to get collisions from child game objects into my parent game object.
You can generate all colliders by script
var collider1 = gameObject.AddComponent<BoxCollider>();
var collider2 = ...
...
then you can set all properties for each collider
https://docs.unity3d.com/ScriptReference/Collider.html
So you can use every collider by it variable as you want
Old but maybe it helps: you can get multiple Components in a gameObject:
Collider[] colls = GetComponents<Collider>();
foreach(var col in colls){
col.isTrigger = false;
}
This believe it or not worked for me, if you're doing 2d. Drag this script onto whatever object has your triggers on it, then drag in your player to the public variable reference slot and make sure you have a BoxCollision2d on it. I'm sure you could use it for 3d you have to change the Collider2D parts though to just "collider" or something (I'm guessing).
The numbers in the array start at 0 and will correspond to whatever BoxCollider is 0th, 1st, and 2nd in the inspector on the right, you can change the number 2 to whatever collider you need.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ur_scriptName: MonoBehaviour
{
public Collider2D[] colliders;
public GameObject player;
void Start()
{
colliders = gameObject.GetComponents<Collider2D>();
}
void Update()
{
}
void OnTriggerEnter2D(Collider2D other)
{
if (colliders[2].IsTouching(player.GetComponent<Collider2D>()))
{
Debug.Log("player touching collider 2");
}
}
}
Kinda surprised nobody's mentioned the easy option!
Declare the collider(s) you want to reference as public variables on the script you wish to access them from, thus:
public BoxCollider2D collider;
Now, in the object inspector, locate the script. It should have a new entry called 'collider' (or whatever you called it). Just drag the collider into that box to link the script variable to the collider.
© 2022 - 2024 — McMap. All rights reserved.
GetComponents
(orGetComponentsInChildren
), but how would you tell them apart? In circumstances like this, I frequently end up attaching an extra script that keeps track of the other components via lists/arrays that I set up in the inspector. – Cusped