Search Unity

Menu controller script not working

Discussion in 'Scripting' started by Dinosaurstic, Jan 27, 2018.

  1. Dinosaurstic

    Dinosaurstic

    Joined:
    Oct 4, 2017
    Posts:
    6
    This C# script is driving me insane. I have script which controls prefabs and, sprites, and index locations for my menus. When you press a panel button, a submenu opens up, and a menu corresponding to the pressed panel is set as a child to the parent. The problem is, you could press buttons in any order, so I have something set up where there are different tabs open for every sublevel, and the different menus can be set as a child to any of the sublevels.

    When closing the sublevels, however, everything messes up. Random sublevels are left up, some icons which depict the open menu don't change properly, and I'm left feeling hollow and empty inside. For a week I've tried fixing it, and I've almost come to the sad realization that this isn't even necessary, and I could replace this code with something much simpler and it wouldn't matter. Here's the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class MenuController : MonoBehaviour {
    7.  
    8.     //Different icons of menu sprites that go on top, on the tabs --By index--
    9.     public Sprite[] icons = new Sprite[6];
    10.  
    11.     //Information of menus, inventory, stats, etc. --By index--
    12.     public GameObject[] menus = new GameObject[6];
    13.  
    14.     //Information on the submenues, the ones with the tabs --By windowsUp--
    15.     public GameObject[] subMenus = new GameObject[6];
    16.  
    17.     //Location of image componenents in submenus --By windowsUp--
    18.     public GameObject[] imgs = new GameObject[6];
    19.  
    20.     //I set the menu information back to this when closing the menus
    21.     public GameObject parentIcon;
    22.  
    23.     //Used so I can control the "Interactable", which moves the UIbox
    24.     public Animator parentAnimator;
    25.  
    26.     //Which submenu the menu is in --By index--
    27.     int[] location = new int[6];
    28.  
    29.     //Which index the current submenu is using --By windowsUp--
    30.     int[] indexLoc = new int[6];
    31.  
    32.     //If the menu is already up --By index--
    33.     bool[] isUsed = new bool[7];
    34.  
    35.     //Which menu is used
    36.     int windowsUp = 0;
    37.    
    38.     void Awake()
    39.     {
    40.         isUsed[6] = false;
    41.     }
    42.  
    43.     public void AddMenu (int index)
    44.     {
    45.         if (!isUsed[index])
    46.         {
    47.             //Stops player from moving
    48.             FindObjectOfType<PlayerMovement>().canMove = false;
    49.  
    50.             //Stops playing from scanning
    51.             FindObjectOfType<PlayerScanner>().canScan = false;
    52.  
    53.             //Stops UIbox from moving too much
    54.             parentAnimator.SetBool("interactable", false);
    55.  
    56.  
    57.             //Moves the subwindow to the front
    58.             subMenus[windowsUp].transform.SetAsLastSibling();
    59.  
    60.             //Sets the submenu's tab icon as what menu is being opened
    61.             imgs[windowsUp].GetComponent<Image>().sprite = icons[index];
    62.  
    63.             //Sets the location of the specific menu
    64.             location[index] = windowsUp;
    65.  
    66.             //Sets the index to whatever window is up
    67.             indexLoc[windowsUp] = index;
    68.  
    69.             //Makes the menu information be a child to the submenu, so it hides when another submenu is focused on
    70.             menus[index].transform.SetParent(subMenus[windowsUp].transform);
    71.  
    72.             //Turns on the menu so it can be seen
    73.             menus[index].SetActive(true);
    74.  
    75.             //Turns on the sub menu so it can be seen
    76.             subMenus[windowsUp].SetActive(true);
    77.  
    78.             //Makes it so we can't open another copy of the same menu
    79.             isUsed[index] = true;
    80.  
    81.             //Incremented for the next possible opened window
    82.             windowsUp++;
    83.         }else
    84.         {
    85.             //Menu is already up, we are just focusing on it
    86.             subMenus[location[index]].transform.SetAsLastSibling();
    87.         }
    88.     }
    89.  
    90.     public void FocusMenu(GameObject submenu)
    91.     {
    92.         submenu.transform.SetAsLastSibling();
    93.     }
    94.  
    95.     public void RemoveMenu(int index)
    96.     {
    97.         //If this is in the first slot, and the window next to it isn't up
    98.         if (location[index] == 0 && !isUsed[ indexLoc[ location[index] + 1] ])
    99.         {
    100.             //Set the last menu as a child under the child manager
    101.             menus[index].transform.SetParent(parentIcon.transform);
    102.  
    103.             //Hides the submenu
    104.             subMenus[0].SetActive(false);
    105.  
    106.             //Hides the menu
    107.             menus[index].SetActive(false);
    108.  
    109.             //The menu isn't being used any more
    110.             isUsed[index] = false;
    111.  
    112.             //There aren't any more windows up
    113.             windowsUp = 0;
    114.  
    115.             FindObjectOfType<PlayerMovement>().canMove = true;
    116.             FindObjectOfType<PlayerScanner>().canScan = true;
    117.             parentAnimator.SetBool("interactable", true);
    118.         }else
    119.         {
    120.             //Sets the deleted menu to be under the menu controller parent
    121.             menus[index].transform.SetParent(parentIcon.transform);
    122.  
    123.             //Disables the deleted menu
    124.             menus[index].SetActive(false);
    125.  
    126.             //Disables the deleted submenu
    127.             subMenus[location[index]].SetActive(false);
    128.  
    129.             //This window isn't being used anymore
    130.             isUsed[index] = false;
    131.  
    132.             //For each submenu after the one we're removing
    133.             for(int i = location[index]; i < 6; i++)
    134.             {
    135.                 //If the window right after our window isn't being used (the window we're closing is the last window)
    136.                 if (i == 5 || !isUsed[indexLoc[i + 1]])
    137.                 {
    138.                     //Sets the submenu before it in front of all the other submenus
    139.                     if(i == location[index] && subMenus[i - 1] != null) { subMenus[i - 1].transform.SetAsLastSibling(); }
    140.  
    141.                     //Makes the menu be a parent to the window below it
    142.                     menus[indexLoc[i]].transform.SetParent(subMenus[i - 1].transform);
    143.  
    144.                     //Fixes the icon of the submenu before it
    145.                     imgs[i - 1].GetComponent<Image>().sprite = icons[indexLoc[i]];
    146.  
    147.                     //Enables the submenu before it
    148.                     subMenus[i - 1].SetActive(true);
    149.  
    150.                     //Sets the current submenu as inactive
    151.                     subMenus[i].SetActive(false);
    152.  
    153.                     //Updates the location
    154.                     location[indexLoc[i]]--;
    155.  
    156.                     //Sets the index to new window
    157.                     indexLoc[i - 1] = indexLoc[i];
    158.  
    159.                     //The latest open window is the one before the closed one now
    160.                     //windowsUp = i - 1;
    161.                     break;
    162.                 }
    163.                 //This isn't the last window
    164.                 else if(!(i == location[index]) && isUsed[indexLoc[i]])
    165.                 {
    166.                     //Makes the menu be a parent to the window below it
    167.                     menus[indexLoc[i]].transform.SetParent(subMenus[i - 1].transform);
    168.  
    169.                     //Fixes the icon of the submenu before it
    170.                     imgs[i - 1].GetComponent<Image>().sprite = icons[indexLoc[i]];
    171.  
    172.                     //Enables the submenu before it
    173.                     subMenus[i - 1].SetActive(true);
    174.  
    175.                     //Sets the current submenu as inactive
    176.                     subMenus[i].SetActive(false);
    177.  
    178.                     //Updates the location
    179.                     location[indexLoc[i]]--;
    180.  
    181.                     //Sets the index to new window
    182.                     indexLoc[i - 1] = indexLoc[i];
    183.                 }
    184.  
    185.                 //The latest open window is the one before the closed one now
    186.                 windowsUp--;
    187.             }
    188. }
    189. }
    190. }