- Home /
How to select one of two Toggle in Toggle Group and receive the value
Premises/what you want to achieve
What I want to achieve is Toggle, A and B in Canvas. If you have selected A, you want to receive the value that you have selected A B is the opposite.
Problem occurring/Error message
I will paste the code below
Applicable source code
The following Managers are attached to the GameManager object, and each serialized object is attached there.
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class TitleManager: MonoBehaviour
{
[SerializeField] private Animator m_HumanAnimator = null;
[SerializeField] private Animator m_CatAnimator = null;
[SerializeField] private Toggle m_ToggleAvater = null;
[SerializeField] private ToggleGroup m_ToggleAvaterGroup = null;
/* MainMenu */
private void Start()
{
m_HumanAnimator = GetComponent<Animator>();
m_CatAnimator = GetComponent<Animator>();
m_ToggleAvater = GetComponent<Toggle>();
m_ToggleAvaterGroup = GetComponent<ToggleGroup>();
}
public void SelectPlayer()
{
if (m_ToggleAvaterGroup.AnyTogglesOn())
{
// Get which player is currently selected
m_ToggleAvater = m_ToggleAvaterGroup.ActiveToggles().FirstOrDefault();
Debug.Log(m_ToggleAvater.name);
}
if (m_ToggleAvater.name == "Human")
{
m_HumanAnimator.SetTrigger("Happy");
GameDataManager.instance.playerTypeID = 0;
Invoke("NextScene", 2);
}
else if (m_ToggleAvater.name == "Cat")
{
m_CatAnimator.SetTrigger("Happy");
GameDataManager.instance.playerTypeID = 1;
Invoke("NextScene", 2);
}
}
void NextScene()
{
SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex + 1);
}
}
I haven't been able to confirm in debug what value the above m_ToggleAvater.name contains. As a result, this transitions to the next scene, and the values such as toggles set in this scene are carried over to the next scene.
I want you to become Actually this is not the case and the toggle is not working, so the function in SelectPlayer() is not running. In addition, this script is I have a button in my UI and I'm calling SelectPlayer() with Onclick() there.
It may be a little wrong in using Onclick, so I would appreciate it if you could let me know.
Supplementary information (FW/tool version, etc.)
Unity 2019.4.3f1