#How you assign button to trigger the DontDestroyOnload(GameObject) in another scene

1 messages · Page 1 of 1 (latest)

worn stratus
#

i want to debuglog the value to check does the value is save eternals when going another scene

#

SceneManager.cs```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneDataManager : MonoBehaviour
{
public static SceneDataManager Instance { get; private set; }

public string Username { get; set; }
public string Password { get; set; }

private void Awake()
{
    if (Instance == null)
    {
        Instance = this;
        DontDestroyOnLoad(gameObject);
    }
    else
    {
        Destroy(gameObject);
    }
}

public void CheckData()
{
    Debug.Log("Username: " + Username + ", Password: " + Password);
}
private void Start()
{
    // Load any saved data here, like high scores or settings
}

private void OnApplicationQuit()
{
    // Save any data here before quitting the game
}

}

#

Login.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class Login : MonoBehaviour
{
    public static Login Instance {private set;get;}
    public TMP_InputField UsernameInput;
    public TMP_InputField PasswordInput;
    public Button LoginButton;
    public GameObject successObject;
    public GameObject errorObject;
    public GameObject errorObject1;
    
    public float displayTime = 2f;
    void Awake() 
    {
        Instance = this;
    }

    void OnDestroy()
    {
        Instance = null;
    }

    //Use this for initialization
    void Start()
    {
        LoginButton.onClick.AddListener(() =>
        {
            StartCoroutine(Mainweb.main.web.Login(UsernameInput.text, PasswordInput.text)); 
        });
    }

    public void ShowSuccessMessage()
    {
        successObject.SetActive(true);
        StartCoroutine(HideMessage(successObject));
    }

    public void ShowErrorMessage()
    {
        errorObject.SetActive(true);
        StartCoroutine(HideMessage(errorObject));
    }
    public void ShowErrorMessage1()
    {
        errorObject1.SetActive(true);
        StartCoroutine(HideMessage(errorObject1));
    }
    IEnumerator HideMessage(GameObject message)
    {
        yield return new WaitForSeconds(displayTime);
        message.SetActive(false);
    }
}
#

do i need adding script for button to get the gameobject?

calm mango
#

You can add a script for your button and have IPointerClickHandler implemented to use the Instance property in your SceneDataManager