- Home /
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!!
Your SceneTransition script ironically has nothing to do with the actual scene loading process. That's what SceneController apparently handles. So the sample code is useless for addressing the actual issue at hand. $$anonymous$$aybe you could post SceneController code ins$$anonymous$$d?
I'm using the same SceneController from the Adventure Game Tutorial here: https://unity3d.com/learn/tutorials/projects/adventure-game-tutorial. The scene loading piece works correctly, but because the string sceneName
is not always what I want it to be, it loads the incorrect scene.
Answer by IsaiahKelly · Sep 10, 2017 at 11:00 PM
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.
Perhaps you're accidentally calling the same script (which always loads scene B) on all buttons?
By "calling the same script" do you mean the actual script SceneTransition.cs or the SceneTransition Component on the object?
I have attached screenshots of my buttons with the SceneTransition script attached. I still have the problem where using the transition loads the "Village" scene from both objects.