Double Colider trigger

Hello there,
I am wondering how you would go about having a trigger that is only set off when BOTH colider are touched.
Ex: you have two game objects, and two coliders. When each of the game objects hit both coliders, it will then trigger a hidden game object. I do not want any objects or coliders getting destroyed. I want the user to be able to do this as many times as he wants.

It’s pretty simple actually here’s a little example:
public GameObject obj1;
public GameObject obj2;
public bool isin1;
public bool isin2;
public GameObject hidden;

void Update(){
if(isin1 == "true" && isin2 == "true"){
Instantiate(hidden, transform.position, transform.rotation) as GameObject
}
}

void OnTriggerStay(Collider hit1){
if(hit1.gameObject.name == "obj1"){
isin1 = true;
}
}

void OnTriggerStay(Collider hit2){
if(hit2.gameObject.name == "obj2"){
isin2 = true;
}
}

Hey, the reason you’re having an issue with BlackWingsAnswer is because you’re using OnTriggerStay twice. You can only have one collider on a game object.

What you need is a script called Detector - which you place on two different gameobjects with colliders on them. Make sure you tick the collider to be a ‘trigger’ (little checkbox in the collider option).

Then, I’ve created a gamelogic script, attach it to a blank gameobject, as you can see, you make a reference to each Detector in the logic, and it asks for two detectors - these are our gameobjects with a collider on them.

You drag both of those Detector objects in the two detector slots on the gamelogic script, and the gamelogic questions each trigger. When both of them are active, you can do something.

public class Detector : MonoBehaviour {

	public	bool	entered	=	false;

	public void OnTriggerEnter ( Collider _collider ) {
		entered = true;
	}


	public void OnTriggerExit ( Collider _collider ) {
		entered = false;
	}
}


public class GameLogic : MonoBehaviour {

	//	References to your detector objects
	public Detector detector1;
	public Detector detector2;

	public void Update () {
		if ( detector1.entered && detector2.entered ) {
			Debug.Log ( "Something is in both triggers" );
		}
	}
}

Those two errors that you’re getting, here’s what they are, btw:

  1. Instantiate(hidden, transform.position, transform.rotation) as GameObject.

When you call Instantiate, you’re telling Unity, ‘I want you to spawn the Object hidden, at this position, as this rotation’ (I don’t know what the hidden object type is). This is fine, Unity will do it. Unity doesn’t much care what the object is, it’ll spawn it. If it has a gameObject attached (which it will), then it’ll be there. In this case, you just remove ‘as GameObject’.

To explain why however, when you use the ‘as GameObject’ at the end, you’re trying to tell Unity that you want to assign a reference of the spawned object to a variable. If you don’t have a variable to assign to, it gives an error. If you had

//  Instantiating an object without storing a reference
Instantiate ( hidden, transform.position, transform.rotation );

//  Storing a reference to the Object, 'as GameObject'
GameObject someObject = Instantiate ( hidden, transform.position, transform.rotation ) as GameObject;

//  Now we have stored someObject in memory if we want to destroy it later from code, or change its colour, etc.

When you give the ‘as GameObject’, you’re saying which script on the thing you’re instantiating you want to reference.

  1. OnTriggerStay(UnityEngine.Collider)

That’s why we need separate scripts for each collider object. They can’t be the same, because Unity only allows one call to OnTriggerStay (). That’s why Unity is saying that it is ‘already defined’.

So hold on, two objects, two triggers and one instantiation when both objects are each in one of the colliders.

Ok, create an empty game object then create two other game object with trigger collider. Make them children of the empty one.

public class ChildrenObject:MonoBehaviour
{
   public bool isFirst;
   public MainObject script;
   void OnTriggerEnter()
   {
      if(isFirst)script.First = true;
      else script.Second = true;
   }
   void OnTriggerExit()
   {
      if(isFirst)script.First = false;
      else script.Second = false;
   }
}

on the main object:

public class MainObject : MonoBehaviour
{
   private bool first;
   private bool second;
   public GameObject obj;
   public bool First
   {
       get{return first;}
       set{first = value;
           if(first && second)Instantiate(obj); 
           first = second = false;
       }
   }
   public bool Second
   {
       get{return second;}
       set{first = value;
           if(first && second)Instantiate(obj); 
       }
   }
}

So, the first script goes on the two object with trigger. All you need is to drag the main object into the script variable, then tick one with isFirst.

Place the second script into the parent object. Drag the object you want to create in the obj slot.

I guess that should do it.