Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SetActive not Displaying a Canvas

Discussion in 'Scripting' started by Nitrox32, Aug 31, 2020.

  1. Nitrox32

    Nitrox32

    Joined:
    May 9, 2017
    Posts:
    161
    I'm adapting Invectors load system for my game to create a scene management system. The system is working great as far as loading the next scene and placing the player at various spawn points. I am trying to add a loading screen that will display while the scene is loading. The player persists from scene to scene and the load canvas is attached to the player so it will be available in every scene. The script to load the next scene is placed on a object and when the player interacts with it it will begin the loading process. The first thing in that process is activate the load scene canvas. The problem is that the canvas doesn't become active. I put a break point after the line that supposed to make it active. Nothing happens. Any thoughts?
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. namespace Invector.Utils
    5. {   [vClassHeader("Load Level", openClose = false)]
    6.     public class vLoadLevel : vMonoBehaviour
    7.     {
    8.         [Tooltip("Write the name of the level you want to load")]
    9.         public string levelToLoad;
    10.         [Tooltip("Assign here the spawnPoint name of the scene that you will load")]
    11.         public string spawnPointName;
    12.  
    13.        
    14.         public Canvas canvas;
    15.      
    16.      
    17.         public void LoadTest()
    18.         {
    19.  
    20.             {
    21.                 canvas.gameObject.SetActive(true);
    22.                 Debug.Break();
    23.                 var thirdPerson = GameObject.FindGameObjectWithTag("Player").transform.gameObject.GetComponent<vCharacterController.vThirdPersonInput>();
    24.                 LoadLevelHelper.LoadScene(levelToLoad, spawnPointName, thirdPerson, canvas);
    25.        
    26.             }
    27.         }
    28.  
    29.  
    30.      
    31.     }
    32. }
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    Where do you call the LoadTest() method from?
     
  3. Nitrox32

    Nitrox32

    Joined:
    May 9, 2017
    Posts:
    161
    I call it from an on OnExecute event using Dialogue System Triggers.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    I don't think SetActive works immediately, but instead queues the object to become active or inactive later in the game loop. You might need to wait a frame to see the results.

    Is your load function synchronous or asynchronous? If it's synchronous, Unity may not get a chance to actually render the frame when the object becomes active before it gets locked up doing the load process.
     
  5. Nitrox32

    Nitrox32

    Joined:
    May 9, 2017
    Posts:
    161
    I was thinking something like that which is why I put the SetActive first in the LoadTest function and the Break just after it. The actual call to load the next scene happens after the Beak with LoadLevelHelper.LoadScene(levelToLoad, spawnPointName, thirdPerson, canvas). That said, the actual load function is asynchronous, but with the break it doesn't actually get to that point.
     
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    The main game loop doesn't continue running while you're paused at a breakpoint. Try stepping forward a frame or two and see if the object becomes active then.

    (This will also start your loading process. If that's a problem for your test, try temporarily disabling that part of the code.)



    The other obvious reason that this could potentially fail is if your variable "canvas" isn't pointed at the right object, so you might want to double-check that also.