I’m new to Unity, so please have in mind that I might miss some basic concept here. I’ll try to describe what is my aim and how I tried to achieve it, and if even the simplest question cross your mind, please share it, as it may be the solution.
I have 2 scenes: Menu and MainScene.
In Menu I have Menu object, which contains 2 Dropdowns and 2 Buttons. To that Menu i have attached script Menu.cs. In that script I have no problem reading value from Dropdown and saving it to a different variable then printing the value by Debug.Log().
In MainScene I have NPC object with Traveling.cs script attached to it. I’d like to pass here the values from Menu.cs script and decide what to do depending on these values. But after I click Next Button which triggers passing values and loads MainScene, I got error:
which points me to this line:
traveling.ReadDropdowns(algorithmName, checkpointsAmount);
Below are codes for both classes:
Traveling.cs:
public class Traveling : MonoBehaviour
{
private int checkpoints;
private string algorithm;
public void ReadDropdowns(string algorithmName, string checkpointsAmount)
{
algorithm = algorithmName;
checkpoints = Convert.ToInt32(checkpointsAmount);
}
}
Menu.cs:
public class Menu : MonoBehaviour
{
List<string> algorithms = new List<string>() { "Insertion", "Brute-force", "Random checkpoints" };
List<string> checkpoints = new List<string>() { "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" };
public Dropdown checkpointsDropdown;
public Dropdown algorithmDropdown;
public string algorithmName;
public string checkpointsAmount;
public Traveling traveling;
void Awake()
{
traveling = GameObject.FindObjectOfType<Traveling>();
}
void Start()
{
algorithmDropdown.AddOptions(algorithms);
checkpointsDropdown.AddOptions(checkpoints);
}
public void dropdownAlgorithm_IndexChanged(int index)
{
algorithmName = algorithms[index];
Debug.Log(algorithmName);
}
public void dropdownCheckpoints_IndexChanged(int index)
{
checkpointsAmount = checkpoints[index];
Debug.Log(checkpointsAmount);
}
public void NextButton()
{
traveling.ReadDropdowns(algorithmName, checkpointsAmount);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
public void QuitButton()
{
Debug.Log("Quit!");
Application.Quit();
}
}
What I am missing here? Please help.