// Update is called once per frame
    private void Update()
    {
        if (!hasPlayerIndex)
        {
            PlayerIndex[] freeControllers = GamePadManager.GetFreeControllers();
            for (int i = 0; i < freeControllers.Length; i++)
            {
                if (freeControllers[i] != index)
                {
                    for (int j = 0; j < GlobalReferences.PlayerStates.Count; j++)
                    {
                        if (GlobalReferences.PlayerStates[i].Index == freeControllers[i])
                        {
                            return;
                        }
                    }

                    for (int l = 0; l < GlobalReferences.PlayerStates.Count; l++)
                    {
                        if (GlobalReferences.PlayerStates[l].Index == index)
                        {
                            index = freeControllers[i];
                            GlobalReferences.PlayerStates[l] = new PlayerState(index, GlobalReferences.PlayerStates[i]);
                            hasPlayerIndex = true;
                            GamePadManager.Connect((int)index);
                            uiScript.CreateUI(index, uiScript.UICanvas.transform);
                            return;
                        }
                    }
                }
                else
                {
                    hasPlayerIndex = true;
                    GamePadManager.Connect((int)index);
                    uiScript.CreateUI(index, uiScript.UICanvas.transform);
                }
            }
        }

        if (hasPlayerIndex)
        {
            GamePadState state = GamePad.GetState(Index);
            if (state.IsConnected)
            {
                if (elapsedImmortal < maxImmortality)
                {
                    healthContainer.isImmortal = true;
                }
                else
                {
                    healthContainer.isImmortal = false;
                }

                UpdateTimers();
                if (isDead)
                {
                    RevivePlayer();
                }
                else
                {
                    if (!Freeze)
                    {
                        Input(state);
                        UpdateRotation();
                    }
                }
                prevState = state;
            }
            else
            {
                uiScript.RemoveUI(index);
                hasPlayerIndex = false;
                GamePadManager.Disconnect(Index);
                GlobalReferences.CurrentGameState = GlobalReferences.GameState.ConnectionLost;
            }
            prevState = state;
        }
        else
        {
            prevState = GamePad.GetState(Index);
        }

        stepTimer += Time.deltaTime;
    }