[SOLVED :) ] Unity Scene Transition with Triggers help

Hey guys, So I’m here with another problem.

I have 2 characters in my game that you can switch with a button.

I want to be able to transition to the 2nd scene after both characters are touching their triggers.

This is my code.

using UnityEngine;
using System.Collections;



public class StageTransitionOne : MonoBehaviour {

    public bool SceneTransitionOne = false;
    public bool SceneTransitionTwo = false;
//    public GameObject OtherSwitch;


    void Start()
    {
//        OtherSwitch.GetComponent<StageCompleteOne>();
    }



    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;
        }

        if ((SceneTransitionOne == true) && (SceneTransitionTwo == true))
        {
            Application.LoadLevel("SceneTwo");
        }


    }

}

I posted a problem like this before that I wasn’t able to fix.

Can anyone help me with this as its alot smaller than what i was asking before.

thank you

What’s happening exactly, is it not triggering?

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.

No it is triggering. What I’m trying to do is.

Each character has their own trigger. I want both characters to reach their trigger before the scene transition. Like if they 2 keys to open a gate.

Character 1 gets his key, character 2 gets her key then they open a gate. I want character 1 to know that character 2 has the key and vice versa.

So I want character 1 to know that character 2 has collided with her trigger and vice versa.

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 :slight_smile:

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;
        }                       
    }       
}

Thanks for helping, but it didn’t solve my problem. Both triggers aren’t communicating with each other. Trigger 1 doesnt know Trigger 2 has been hit.

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;
        } 
    }
}

Also, tag comparisons should be done with CompareTag, not with tag ==

So line 13. should be:
if (collis.CompareTag(validPlayer))

LOL! Looks like CG_Echtzeitschmiede beat me to the punch :wink:

@krougeau
IT WORKS!!!1 Thank you so much!!! Thank you so much!!! Thank you!!

I’m still a beginner, so I’m just getting the hang of things. I’m gonna study the code so I dont get lost again. Thank you.

@CG_Echtzeitschmiede
Thanks for helping. It’s works too. :slight_smile:

Thank you guys. I’m so happy.

1 Like

Awesome, glad it worked out :wink: Have fun!