How would I be able to store a value that can be used later with a function that returns void?

What I’m trying to do might be unnecessarily convoluted, and if anyone has a better idea on how to approach this, please feel free to share but do make sure to explain the process.

Essentially what I’m trying to do is after a GameObject button is clicked, I want some value to be stored somewhere. Whether it be a string or int, I just need something to identify that that button was clicked and I have the information for it. After that, I’m trying to spawn a GameObject on a different scene depending on which button was clicked, which is why I need that information stored to be able to identify which button it was.

The issue I’m running into is my function returns void, so I can’t really store any values that aren’t immediately dropped once the function has finished it’s task.

I’m using three scripts for this. The first script contains the TaskOnClick function, the second script contains a class that I would want the data to be stored in (which may not be necessary), and the third script generates the gameobject depending on what data was stored. I will link the scripts below.

Again, if anyone has a better idea as to how to approach this task I’m trying to complete I’m definitely open to suggestions. At the end of the day, I just want this to work in a way thats understandable and reproducable.

Script #1 (TaskOnClick function):

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class WarriorPicked : MonoBehaviour
{
    public Button WarriorButton;
    PlayerStats ClassSelected;



    void Start()
    {
        Button btn = WarriorButton.GetComponent<Button>();
        btn.onClick.AddListener(TaskOnClick);
        Debug.Log("Script working");

    }

    void TaskOnClick()
    {
        ClassSelected.ClassSelectedString = "Warrior";
        Debug.Log("Player class selected as warrior.");
    }

}

Script #2 (Data storage class, I need to be able to modify the data in this class after the click function):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerStats : ScriptableObject
{
    public string ClassSelectedString;

}

Script #3 (Generate GameObject):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class GenerateSelection1 : MonoBehaviour
{
    PlayerStats GenerateClass;
    void Start()
    {
        Debug.Log("Unit selection screen entered.");
        GenerateClass.ClassSelectedString = gameObject.GetComponent<PlayerStats>().ClassSelectedString;
        Debug.Log(GenerateClass.ClassSelectedString);
    }
}

Anyway, I get a bunch of errors when the new scene is loaded. Probably a lot going on here but I would appreciate any help/suggestions.

Ok, if this just needs to move variables from one scene to another, there are a couple of different ways I would consider doing it.

The quickest and easiest would be to use PlayerPrefs.

In your code for the button click, do something like…

PlayerPrefs.SetString("SelectedClass", "Warrior");

then in your code in the next scene, retrieve the selected class with …

string SelectedClass = PlayerPrefs.GetString("SelectedClass");

Then you can do whatever you need based on the class selected.

Another way to accomplish this, especially if you want to move a lot of data between scenes, would be to create a script whose sole purpose is to hold data. Something like this…

using UnityEngine;
using UnityEngine.Advertisements;

public class SelectedOptions : MonoBehaviour
{
    public string SelectedClass;


  void Start()
  {
    DontDestroyOnLoad(gameobject);
  }
}

Make sure this script is in a scene that loads only one time, at launch.

Then in any script in any scene that needs the data do this…

SelectedOptions SO;

void Start()
{
  SO=FindObjectOfType<SelectedOptions>();
}

Then when you want to access the data…

if (SO.SelectedClass=="Warrior")
{
   //Warrior Code Here
}

With this method, add as many public variables as you want to the SelectedOptions script, and access them from wherever you need.

Hope this helps,
-Larry