Search Unity

SetActive(true) and SetActive(false) not working as expected [Solved]

Discussion in 'Scripting' started by Paul-Swanson, Jan 11, 2019.

  1. Paul-Swanson

    Paul-Swanson

    Joined:
    Jan 22, 2014
    Posts:
    210
    Hi all.
    I'm having trouble setting up my Menu and Pause canvas
    This is weird my code worked before. Then it got recompiled randomly.
    I simply cannot get Pause UI to show up when I press P it also doesn't change my TimeScale...it used to...
    And same with my Menu Screen...When I press U it unlocks my Cursor via another script totally like expected, but won't show my Menu UI screen.

    Iv tried changing the order of my MenuOn\Off and Pause On\Off methods...nothing seems to work.

    <Note> I'm well aware if doing this very unoptimized...my main concern is getting the damn thing to work properly, I'll simplify and optimize it later. Also there are Zero Compiler errors, so it must be HOW I'm doing this...
    </Note>

    I have no idea why Saving in the editor and coming back the next day would cause something that previous worked fine to suddenly ...just not. The drone stuff works perfectly fine, And im pretty much doing the exact same thing, Setting to False then Setting to Active when i want it back...[this is why I left my Drone Stuff in there, its mildly relevant]

    Id greatly appreciate any help with this. Since I'm Stuck.



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using CoverShooter;
    5.  
    6. public class MenuPlayer : MonoBehaviour {
    7.  
    8.     public bool Paused = false;
    9.     public bool Menu = false;
    10.     public ThirdPersonInput Player;
    11.     public GameObject PauseCanvas;
    12.     public GameObject MenuCanvas;
    13.     public Camera PlayerCamera;
    14.     public GameObject DroneCamera;
    15.     public GameObject DroneSet;
    16.     public Transform DroneSpawnPoint;
    17.     public Transform DroneCameraSpawnPoint;
    18.     private GameObject DroneCameraGO;
    19.     private GameObject DroneObjectGO;
    20.     public string DroneCameraTag = "DroneCamera";
    21.     public string DroneObjectTag = "PlayerDrone";
    22.  
    23.  
    24.     // Use this for initialization
    25.     void Start()
    26.     {
    27.         Paused = false;
    28.         Menu = false;
    29.         DroneCameraOn = false;
    30.         PauseCanvas.gameObject.SetActive(false);
    31.         MenuCanvas.gameObject.SetActive(false);
    32.         PlayerCamera.gameObject.SetActive(true);
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void LateUpdate()
    37.     {
    38.         if (Input.GetKeyDown(KeyCode.P))
    39.         {
    40.             Pause();
    41.         }
    42.         if (Input.GetKeyDown(KeyCode.U))
    43.         {
    44.             MenuScreen();
    45.         }
    46.         if (Input.GetKeyDown(KeyCode.O))
    47.         {
    48.             DroneActivation();
    49.         }
    50.     }
    51.  
    52.     void Pause()
    53.     {
    54.         if ((Paused != true) && (Menu != true) && (DroneCameraOn != false))
    55.         {
    56.             PausedOn();
    57.         }
    58.         else
    59.         {
    60.             PausedOff();
    61.         }
    62.     }
    63.  
    64.     void MenuScreen()
    65.     {
    66.         if ((Menu != true) && (Paused != true) && (DroneCameraOn != false))
    67.         {
    68.             TurnOnMenu();
    69.         }
    70.         else
    71.         {
    72.             TurnOffMenu();
    73.         }
    74.     }
    75.  
    76.     private void PausedOn()
    77.     {
    78.         PauseCanvas.gameObject.SetActive(true);
    79.         Time.timeScale = .000001f;
    80.         Player.isPause = !Player.isPause;
    81.         Paused = !Paused;
    82.     }
    83.  
    84.     private void PausedOff()
    85.     {
    86.         PauseCanvas.gameObject.SetActive(false);
    87.         Time.timeScale = 1;
    88.         Player.isPause = !Player.isPause;
    89.         Paused = !Paused;
    90.     }
    91.  
    92.     public void TurnOnMenu()
    93.     {
    94.         MenuCanvas.gameObject.SetActive(true);
    95.         Menu = true;
    96.         Player.isPause = !Player.isPause;
    97.         Player.isMenu = !Player.isMenu;
    98.     }
    99.  
    100.     public void TurnOffMenu()
    101.     {
    102.         MenuCanvas.gameObject.SetActive(false);
    103.         Menu = false;
    104.         Player.isPause = !Player.isPause;
    105.         Player.isMenu = !Player.isMenu;
    106.     }
    107.  
    108.     public void DroneActivation()
    109.     {
    110.         if (DroneCameraOn == false)
    111.         {
    112.             Player.isPause = !Player.isPause;
    113.             DroneCameraOn = !DroneCameraOn;
    114.             DroneInstantiation();
    115.         }
    116.         else
    117.         {
    118.             Player.isPause = !Player.isPause;
    119.             DroneCameraOn = !DroneCameraOn;
    120.             PauseCanvas.gameObject.SetActive(false);
    121.             DroneCameraGO.gameObject.SetActive(false);
    122.             DestroyDroneStuff();
    123.         }
    124.     }
    125.  
    126.     public void DroneInstantiation()
    127.     {
    128.         Instantiate(DroneCamera, DroneCameraSpawnPoint.position, DroneCameraSpawnPoint.rotation);
    129.         Instantiate(DroneSet, DroneSpawnPoint.position, DroneSpawnPoint.rotation);
    130.         FindDroneStuff();
    131.     }
    132.  
    133.     public void FindDroneStuff()
    134.     {
    135.         DroneCameraGO = GameObject.FindWithTag(DroneCameraTag);
    136.         DroneObjectGO = GameObject.FindWithTag(DroneObjectTag);
    137.         DroneOnCameraStates();
    138.     }
    139.  
    140.     public void DroneOnCameraStates()
    141.     {
    142.         PlayerCamera.gameObject.SetActive(false);
    143.         DroneCameraGO.gameObject.SetActive(true);
    144.     }
    145.  
    146.     public void DestroyDroneStuff()
    147.     {
    148.         Destroy(DroneCameraGO);
    149.         Destroy(DroneObjectGO);
    150.         PlayerCamera.gameObject.SetActive(true);
    151.     }
    152.  
    153.  
    154. }
     
    Last edited: Jan 12, 2019
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    3,247
    Right click on any one of your C# files and do a reimport of just that file, see if perhaps it isn't recompiling when you think it is.
     
  3. Paul-Swanson

    Paul-Swanson

    Joined:
    Jan 22, 2014
    Posts:
    210
    I figured it out actually, somehow it didn't save my previous edit and this was the offending line:
    if ((Paused != true) && (Menu != true) && (DroneCameraOn != false))

    Needed to be changed to:
    if ((Paused != true) && (Menu != true) || (DroneCameraOn != false))

    Marking this as solved.
    Thanks for the reply though, I'll try that in the future.