[Question] Unity C# Set a Player Class and refence issue
Hi there you all I have been working to understand this and work on it but I couldnt do it so here I am asking for help. There is 3 Class for my game in UI (warrior , archer ,wizard) , all of them buttons and toggle in them , when you click the button it actives the toggle in group and player can know which class he chosed. The issue is I have another scene for Character and Inventory but I can not reference the toggle and so can not get data which class player chosed.
public class ToggleScript : MonoBehaviour
{
public Button currentButton;
public Toggle currentToggle;
public AudioSource buttonclicksound;
public ToggleGroup ToggleGroup;
public static string classname;
public static ToggleScript toggleScript;
private void Start()
{
buttonclicksound.GetComponent<AudioSource>();
currentButton.GetComponent<Button>();
currentToggle.GetComponent<Toggle>();
currentButton.onClick.AddListener(OnClick);
ToggleGroup.GetComponent<ToggleGroup>();
}
public void OnClick()
{
Toggle selectedToggle = ToggleGroup.ActiveToggles().FirstOrDefault();
if (selectedToggle != null)
Debug.Log(selectedToggle, selectedToggle);
buttonclicksound.Play();
currentToggle.isOn = true;
classname = selectedToggle.ToString();
}
So here is toggle script I currently use I know its not clean that much but It keeps work selectedToggle is the player last chosed toggle and It shows in console So I transfer it to him string and put it in static public string so I could take this string from another scene in script and I can detect what player chose but I cant do the reference.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SetCharacter : MonoBehaviour
{
string name;
public void Start()
{
name = ToggleScript.classname;
Debug.Log(name);
}
}
It doesnt work I get error of unity object reference not set to an instance of an object , any ideas or solution ?