1

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.

7
  • 1
    Are you able to step through the code using a debugger? Based on the logic it seems possible that the code is working exactly as designed and you're calling both ActivateTabs and DeactivateTabs every time, which would result in no visual change. Can you confirm? Commented Jan 27 at 19:34
  • Oh, thanks didn't notice that Commented Jan 27 at 19:46
  • ActivateTabs(group); and DeactivateTabs(group); use the same group (the parameter to the function). You want to call ActivateTabs on one group and DeactivateTabs on the other. Commented Jan 27 at 19:49
  • Do you know how to change string in to CanvasGroup? Commented Jan 27 at 19:52
  • 1
    @001 note that string.Equals gives you a bit more control with additional parameters though where == just uses the most common Commented Jan 27 at 20:26

1 Answer 1

3

The main issue is you pass the same group to ActivateTabs(group); and DeactivateTabs(group); So, instead of using strings, compare the groups directly.

private void EfficientTabChange(CanvasGroup group)
{
    CanvasGroup [] groups = new CanvasGroup[2] { hydrogenScreen, heliumScreen };
    
    foreach (CanvasGroup g in groups)
    {
        if (g == group)
        {
            ActivateTabs(g);
        }
        else
        {
            DeactivateTabs(g);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.