Conditions on buttons

Hi there,

I’m fairly new to unity and not familiar with scripting. Here is my problem:
I have a button that enables to load the next scene. Now I would like to have this scene-loading script only to work if the player has clicked on another button in a previous scene… How can I achieve this?

I know, that there are many related questions to this one on this forum but since Im not very good at scripting, I’m not able to adapt the proposed scripts in other solutions to my problem.

Hope you can help me out! thanks in advance! :wink:

There are a number of ways to accomplish this. One would be to create a persistent empty gameobject throughout the scenes that has a script to store relevant actions in previous scenes.

For example, using your layout the empty object script could look like;
using UnityEngine;
using System.Collections;

public class ButtonTracker : MonoBehaviour {
    public bool pressed = false; //boolean variable to change when button pressed
    
    void Awake() {
        DontDestroyOnLoad(transform.gameObject); //persists your gameobject through scenes
    }
    public void ButtonPressed(){
        pressed = true;
    }
}

where you call the ButtonPressed method on the onClick event on the button.

Then in your script that loads the scene you check whether the bool is true:

    using UnityEngine;
    using System.Collections;
    
    public class YourClass : MonoBehaviour {
        public GameObject buttonPersistance
        
        void Start() {
            // find the persistant gameobject
            buttonPersistance= GameObject.Find("whatever you called it");
        }

        public void yourLoadSceneMethod{
             // get aqccess to script on gameobject
             ButtonTracker buttonTrack = buttonPersistance.GetComponent<ButtonTracker>();

             //use the bool to determine whether button in previous scene has been pressed
             if (buttonTrack.pressed){
                // do your load scene level code
             }
              else{
                   //do something else
              }
        }
    }

I hope that helps :slight_smile:

Thank you very much for your Help!! :wink: