- Home /
How to set isTrigger to false of two colliders in 1 game object
hi, I created one game object with two box colliders(A and B). both these colliders are set isTrigger to true in the inspector and I would like to set these two colliders (isTrigger) into false when the player enter a different collider (C). however only one collider(A) is set to isTrigger into false when i run the below code. can anyone please tell how can i set both colliders(A and B) to false. Thank you.
bool Ans = false; public GameObject Fig ; public GameObject figCol; public BoxCollider ansCol;
public AudioClip endGame;
public AudioClip StartAns;
void OnTriggerEnter( Collider col)
{
if (Ans == false && col.gameObject.tag == "Player")
{ ansCol.isTrigger = false;
audio.clip = StartAns;
audio.Play ();
StartCoroutine ("AudioStart");
} // end if
}//end function on trigger enter
Answer by _joe_ · Apr 03, 2015 at 12:48 PM
That wouldn't really work, The trigger that is referenced will always return the first one in the inspector.
The better approach is to add 2 empty children GameObjects. Attach a collider to each.
And test for both children in the OnTriggerEnter for each collider. If you want more control, attach scripts to each child and access it using.
col.gameObject.GetComponent().doWhatever();
thank you for everyone. I will try to do as joe suggested.I thought that we can access all thecomponents in one game object.i did came across using array to get the component but i'm confused how it works. anyway thanks once again.
Great let me know, and please accept the answer if it helped :)
Answer by sparkzbarca · Apr 03, 2015 at 12:44 PM
you need to google unity compound colliders.
if you want to use two on a single game object they can't be the same type of primitive.
Using a box and a sphere works, but not two boxes.
if you use a box and a sphere you can reference them seperately through get component
getcomponent<BoxCollider>()
getcomponent<SphereCollider>()