- Home /
I have list with 16 Toggles in it. Player should be able to always have X active. If he clicks X+1, the oldest toggle should switch off. What did I miss?
Hi :)
I have a List with 16 Toggles. The Player should be able to have X active at any given moment - let's say X=2.
I want the program to automatically de-activate the oldest entry of "toggled on"-Toggles in the List and set the clicked Toggle to active once the player tries to enabled a third toggle (or X+1).
This kind of works. I can ativate up to two Toggles and when I click on a different one, the oldest gets set to isOn=false.
BUT: This does not work if I click on a preveriously checked toggle which had been set to false by code. If I want to re-activate, I need to manuall uncheck an active Toggle and click on a previously checked toggle. I don't know what causes this behaviour.
This is the function which is called when a toggle gets clicked:
public class CraftingRecipeUI
{
[SerializeField] CraftingWindow craftingWindow;
[...]
public void ToggleChanged(bool toggle)
{
//if (craftingWindow.mitarbeiterTemp <= betrieb.MitarbeiterAnzahl)
//{
if (toggle == true)
{
craftingWindow.mitarbeiterTemp++;
Debug.Log("Mitarbeiter Temp: " + craftingWindow.mitarbeiterTemp);
}
if (toggle == false)
{
craftingWindow.mitarbeiterTemp--;
Debug.Log("Mitarbeiter Temp: " + craftingWindow.mitarbeiterTemp);
if (craftingWindow.mitarbeiterTemp < 0)
{
craftingWindow.mitarbeiterTemp = 0;
}
}
craftingWindow.Check();
//}
}
}
And here's the Check()-Function:
public class CraftingWindow : MonoBehaviour
{
public List<Toggle> MitarbeiterArbeitsplatz;
public int mitarbeiterTemp;
public Betrieb betrieb;
[...]
public void Check()
{
if (mitarbeiterTemp > betrieb.MitarbeiterAnzahl)
{
Toggle firstArbeitskraft = MitarbeiterArbeitsplatz.Find(x => x.isOn == true);
//firstArbeitskraft.GetComponent<Button>().onClick.Invoke(); (I tried if maybe something would change if I made the code click the button, but it changed nothing)
firstArbeitskraft.isOn = false;
}
}
[...]
}
I'd appreciate your help. I think I might miss a check for some value somwhere, but I'm not sure about where to go from here.
Here's a video I made of how it looks like: link to a youtube video I uploaded
But when I use the toggle group element, only one thing can be active. That's why I needed to write code. Or do I misunderstand you?
Answer by Hellium · Feb 09, 2021 at 04:23 PM
The following is a rework of your code. You have some mixed up code IMHO. The responsibility to manage your toggles is split into two classes.
FOLLOWING CODE NOT TESTED
public class CraftingRecipeUI
{
[SerializeField] private Toggle[] toggles;
[SerializeField, Min(1)] private int maxEnabledTogglesCount = 2;
private List<Toggle> enabledToggles = new List<Toggle>();
private void Start()
{
for(int toggleIndex = 0 ; toggleIndex < toggles.Length ; ++toggleIndex)
{
Toggle toggle = toggles[toggleIndex];
toggle.onValueChanged.AddListener(value => ToggleValueChanged(toggle, value));
}
}
// Remove any call to this method from the toggles in the inspector
private void ToggleChanged(Toggle toggle, bool value)
{
if(value)
{
enabledToggles.Add(toggle);
if(enabledToggles.Count > maxEnabledTogglesCount)
{
enabledToggles[0].isOn = false; // This should remove the toggle from the list automatically
}
}
else
{
enabledToggles.Remove(toggle);
}
}
}
And taht's it. You won't need the code from the CraftingWindow
class you've provided.
Thank you so much! Looks like I was absolutely on the wrong path with my idea. This worked just like what I tried to accomplish and looks so much cleaner, too!
Your answer
Follow this Question
Related Questions
List Transform returns null 2 Answers
Modifying Lists via the inspector 1 Answer
Toggle button don't dissapear.. 1 Answer
Toggles/Toggle Groups Question 0 Answers