| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- public class BackButton : MonoBehaviour
- {
- [Header("Target Scene")]
- public string targetSceneName = "TitleScreen";
- [Header("Button Reference")]
- public Button backButton;
- private void Awake()
- {
- // If no button is assigned, try to find it on this GameObject
- if (backButton == null)
- {
- backButton = GetComponent<Button>();
- }
- // If still no button found, try to find one in children
- if (backButton == null)
- {
- backButton = GetComponentInChildren<Button>();
- }
- if (backButton != null)
- {
- backButton.onClick.AddListener(GoBack);
- }
- else
- {
- Debug.LogWarning("No Button component found for BackButton script on " + gameObject.name);
- }
- }
- private void GoBack()
- {
- Debug.Log($"Going back to scene: {targetSceneName}");
- SceneManager.LoadScene(targetSceneName);
- }
- // Method to set target scene programmatically
- public void SetTargetScene(string sceneName)
- {
- targetSceneName = sceneName;
- }
- }
|