Search Unity

how to pass bool value form previous scene to new scene ,

Discussion in 'Scripting' started by ashishkushwaha, Jul 19, 2019.

  1. ashishkushwaha

    ashishkushwaha

    Joined:
    May 12, 2015
    Posts:
    35
    Code (CSharp):
    1. public class RewardPannelTesting : MonoBehaviour
    2. {
    3.  
    4.     public bool a = false;
    5.     GameObject rewardpannelobject;
    6.     // Start is called before the first frame update
    7.     void Start()
    8.     {
    9.         rewardpannelobject = GameObject.Find("Canvas");
    10.        
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         rewardPannel();
    17.     }
    18.     public void rewardPannel()
    19.     {
    20.      
    21.         if (a==true)
    22.         {
    23.             rewardpannelobject.transform.GetChild(4).gameObject.SetActive(true);
    24.         }
    25.     }
    26.     public void rewardPannelDeactive()
    27.     {
    28.         a = false;
    29.         rewardpannelobject.transform.GetChild(4).gameObject.SetActive(false);
    30.     }
    31.    
    32. }
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class EndTrigger : MonoBehaviour
    6.  
    7.  
    8. {
    9.     //public  RewardPannelTesting pannelTesting;
    10.     RewardPannelTesting reward;
    11.     GameObject player;
    12.     int amount = 1;
    13.  
    14.  
    15.     //  public GameManager gameManager;
    16.     private void Awake()
    17.     {
    18.         player = GameObject.FindGameObjectWithTag("Player"); // find player when it hit the trigger
    19.     }
    20.     private void OnTriggerEnter(Collider other)
    21.     {
    22.         if (other.tag == "Player")
    23.         {
    24.             reward.a = true;
    25.             SceneManager.LoadScene("MainMenuCurrent");
    26.          
    27.          
    28.         }
    29.     }
    30. }
    how to pass bool value form previous scene to new scene , note { previous scene get destroyed before loading new scene } describe my situation :-
    i am hitting a trigger call "END trigger" and trying to pass a bool value true for new scene
    (accessing another script , "reward panel testing " script to activate reward panel game object..

    for activation bool A is responsible ... please help :(
     

    Attached Files:

    Last edited: Jul 19, 2019
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    The absolute most simple way is to make public static bool that both scripts look at.
    Code (csharp):
    1. public Scene1Script : Mono
    2. {
    3.     public static bool boolForScene2 = false; // Static variables belong to a type, NOT instances of that type. All instances share it; if any change it, it will change for all of them.
    4.  
    5.     private void CallMeBeforeSwitchingScenes()
    6.     {
    7.           boolForScene2 = true;
    8.     }
    9. }
    10.  
    11. public Scene2Script : Mono
    12. {
    13.     private void Awake()
    14.     {
    15.         if(Scene1Script.boolForScene2 == true) // Note that this refers to the type, NOT an instance of the type
    16.             Debug.Log('Yay!);
    17.    }
    18. }
     
    ashishkushwaha likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You can pass a GameObject between scenes with whatever data you want in attached MonoBehaviour scripts. You do this by setting DontDestroyOnLoad. Google it for example.

    Static variables as above are also good.
     
    ashishkushwaha likes this.
  4. ashishkushwaha

    ashishkushwaha

    Joined:
    May 12, 2015
    Posts:
    35
    thanks for the valuable time you gave its working !