I'm new to C#. I'm trying to make game in Unity. These functions are necessary for changing in-game tabs with help of a button.
public string check;
public CanvasGroup hydrogenScreen;
public CanvasGroup heliumScreen;
private void EfficientTabChange(CanvasGroup group)
{
string[] groupName = new string[2] { "hydrogenScreen" , "heliumScreen"};
check = group.ToString();
foreach (string nos in groupName)
{
if (check.Equals(nos))
{
ActivateTabs(group);
}
else
{
DeactivateTabs(group);
}
}
}
public void SwitchTabs(int id)
{
switch (id)
{
case 0:
EfficientTabChange(hydrogenScreen);
break;
case 1:
EfficientTabChange(heliumScreen);
}
}
private void ActivateTabs(CanvasGroup group)
{
group.alpha = 1;
group.interactable = true;
group.blocksRaycasts = true;
}
private void DeactivateTabs(CanvasGroup group)
{
group.alpha = 0;
group.interactable = false;
group.blocksRaycasts = false;
}
I don't know what is the problem in here. I'm just checking if the CanvasGroup is one of the names in array to find the right tab to change.
ActivateTabsandDeactivateTabsevery time, which would result in no visual change. Can you confirm?ActivateTabs(group);andDeactivateTabs(group);use the samegroup(the parameter to the function). You want to callActivateTabson one group andDeactivateTabson the other.string.Equalsgives you a bit more control with additional parameters though where==just uses the most common