Switching Scenes Via Button Only Works for One Transition

Hi everyone,

I am attempting to make a game that allows the character to go to different scenes based on what they run into. I created a canvas that appears OnTriggerEnter that asks the player if they want to go to a certain scene, with buttons to confirm or deny (the functions for the onClick set in the Unity Inspector). This works correctly when I only have one transition in the scene. However, if I have two different objects that will take me to two different scenes, I can only ever go to one of the scenes. For example, if object A should take me to scene B and object C should take me to scene D, both objects will take me to scene B. I have tried this in multiple different ways, a canvas object for each transition object, one canvas object that just changes the data, assigning the button OnClicks in the script, etc., and none of it lets me transition correctly. When I debug the script, the sceneName is always the same, whether it is for object A or C. I have included the code for my transition script:

public class SceneTransition : MonoBehaviour
{

	public string message;
	public CanvasGroup textDisplayCanvas;
	public UnityStandardAssets.Characters.FirstPerson.MouseLook character;
	public CanvasGroup faderCanvasGroup;
	public string sceneName;

	// Use this for initialization
	void Start ()
	{
		textDisplayCanvas.alpha = 0;
	}
	
	// Update is called once per frame
	void Update ()
	{
		
	}

	void OnTriggerEnter (Collider col)
	{
		character.SetCursorLock (true);
		Text[] messages = textDisplayCanvas.GetComponentsInChildren<Text> ();
		messages [0].text = message;
		textDisplayCanvas.alpha = 1;
	}

	public void CancelTransition ()
	{
		character.SetCursorLock (false);
		textDisplayCanvas.alpha = 0;
	}

	public void TransitionToScene ()
	{
		textDisplayCanvas.alpha = 0;
		SceneController sceneController = FindObjectOfType<SceneController> ();
		sceneController.FadeAndLoadScene (sceneName);
	}
}

Thanks for your help!!

If you’re using the SceneController script from the adventure game tutorial then you must have modified the FadeAndLoadScene method to accept a string instead of a SceneReaction object. So I assume that method looks something like this now:

    public void FadeAndLoadScene(string sceneName)
    {
        // If a fade isn't happening then start fading and switching scenes.
        if (!isFading)
        {
            StartCoroutine(FadeAndSwitchScenes(sceneName));
        }
    }

This method along with your SceneTransition script works perfectly fine for me. In my tests I added and set a new SceneTransition script for each UI button like so.

101623-scene-transition-event.png

Perhaps you’re accidentally calling the same script (which always loads scene B) on all buttons?