If you haven’t already, you should debug the code. Put a Debug.Log() in each of if statements controlling the detection to ensure that it’s actually being picked up first.
Hmmm… Looking at your example, I’d suggest a couple of alterations. First, let’s get rid of the unnecessary double parenthesis. Next, let’s move the check to see if both bools return true from OnTriggerEnter to Update so that it’s processed as soon as it happens. I’m not certain this will solve your problem, but it couldn’t hurt to try
using UnityEngine;
using System.Collections;
public class StageTransitionOne : MonoBehaviour
{
public bool SceneTransitionOne = false;
public bool SceneTransitionTwo = false;
private int ticks = 0; // simple counter (I use this for everything)
void Update()
{
if (SceneTransitionOne == true && SceneTransitionTwo == true && ticks == 0)
{
ticks++; // add 1 to our ticks counter so that this code is only processed once
Application.LoadLevel("SceneTwo");
}
}
void OnTriggerEnter (Collider collis)
{
Debug.Log ("Collided with: " + gameObject.tag); // important debugging aid!
if (gameObject.tag == "TarroStageOne")
{
SceneTransitionOne = true;
}
if (gameObject.tag == "KeannaStageOne")
{
SceneTransitionTwo = true;
}
}
}
Oh geez… I think I know what the issue is, but let me clarify to be sure… I was under the impression that this code was on a single object that both characters were interacting with… If there are two separate objects, and each character touches a different one, then both values will never be true since both characters aren’t touching the same trigger. In that case, you’ll need to have one trigger tell the other trigger (through BroadcastMessage or whatever interscript communication option you prefer) that it’s value has been set to true. I can work up a quick example if you need.
Yeah they’re on 2 different triggers. I’ll really appreciate an example. \
The characters are on 2 different sides of a stage. When one gets to their trigger, I dont want him to be able to leave the stage until the 2nd character gets to their trigger.
They both have to get to their goals before exiting the stage. Hence 2 different triggers.
I think the following approach may work for you. You would put the script on your two trigger objects, NOT on the characters! In the Unity inspector, for each of the trigger objects you would specify the validPlayer string that this particular trigger is intended for.
public class StageTransitionOne : MonoBehaviour
{
public bool triggered = false;
public string validPlayer = "";
private StageTransitionOne[] allTransitionScripts;
void Start()
{
allTransitionScripts = FindObjectsOfType(typeof(StageTransitionOne)) as StageTransitionOne[];
}
void OnTriggerEnter (Collider collis)
{
if ((collis.tag == validPlayer))
{
triggered = true;
}
foreach (StageTransitionOne currentScript in allTransitionScripts)
{
if (!currentScript.triggered)
return;
}
Application.LoadLevel("SceneTwo");
}
}
See how this works for ya… Put this on both triggers.
using UnityEngine;
using System.Collections;
public class StageTransitionOne : MonoBehaviour
{
public GameObject trigger1; // assign in the Inspector
public GameObject trigger2; // assign in the Inspector
public bool SceneTransitionOne = false;
public bool SceneTransitionTwo = false;
private int ticks = 0; // simple counter (I use this for everything)
void Update()
{
if (SceneTransitionOne == true && SceneTransitionTwo == true && ticks == 0 && this.gameObject == trigger1) // added this last part so that both triggers don't try to load the new level at the same time. Only trigger 1 will switch scenes
{
ticks++; // add 1 to our ticks counter so that this code is only processed once
Application.LoadLevel("SceneTwo");
}
}
void OnTriggerEnter (Collider collis)
{
Debug.Log ("Collided with: " + gameObject.tag); // important debugging aid!
if (gameObject.tag == "TarroStageOne")
{
SceneTransitionOne = true;
if(this.gameObject == trigger1) // if this is trigger #1,
{
trigger2.BroadcastMessage("OtherTrigger", gameObject.tag); // tell trigger #2 what happend.
}
if(this.gameObject == trigger2) // if this is trigger #2,
{
trigger1.BroadcastMessage("OtherTrigger", gameObject.tag); // tell trigger #1 what happend.
}
}
if (gameObject.tag == "KeannaStageOne")
{
SceneTransitionTwo = true;
if(this.gameObject == trigger1) // if this is trigger #1,
{
trigger2.BroadcastMessage("OtherTrigger", gameObject.tag); // tell trigger #2 what happend
}
if(this.gameObject == trigger2) // if this is trigger #2,
{
trigger1.BroadcastMessage("OtherTrigger", gameObject.tag); // tell trigger #1 what happend
}
}
}
public void OtherTrigger(string tag) // this gets called by the "other" trigger in the scene
{
if(tag == "TarroStageOne")
{
SceneTransitionOne = true;
}
if (tag == "KeannaStageOne")
{
SceneTransitionTwo = true;
}
}
}