Search Unity

Bug SetActive(true) works on PC editor but not Mac editor.

Discussion in 'Scripting' started by EOMedvis, Jul 20, 2021.

  1. EOMedvis

    EOMedvis

    Joined:
    Feb 19, 2019
    Posts:
    94
    I'm running into a very peculiar problem with a simple script. I have a function that uses SetActive(true) to enable a game object, that's being executed from another game object that is always active. But it only works on Windows Unity editor, but not a Mac Unity editor for some reason.


    Code (CSharp):
    1. IEnumerator FadeOutSplash() //fades out splash screen then runs the starting sequence.
    2.     {
    3.         while (true)
    4.         {
    5.             splashPage.GetComponent<CanvasGroup>().alpha -= fadeAmount;
    6.  
    7.             if (splashPage.GetComponent<CanvasGroup>().alpha <= 0f)
    8.             {
    9.                 splashPage.GetComponent<CanvasGroup>().alpha = 0f;
    10.                 break;
    11.             }
    12.  
    13.             yield return new WaitForEndOfFrame();
    14.         }
    15.  
    16.         splashPage.SetActive(false);
    17.         SaveNLoad.Load();      
    18.  
    19.         if (GameManager.CurrentFile.AppStats.Launches == 0)
    20.         {
    21.             videoPlayerObj.SetActive(true); //<===== problem here
    22.             videoPlayer.PlayVideo();
    23.             StartCoroutine(VideoTimer());
    24.         }
    25.         else
    26.         {
    27.             StartUpdate();
    28.         }
    29.     }  
    The issue is marked on the code above. Only that line fails. Every line below it works on both platforms. The object being reference is correct and is present in the scene (just disabled).

    Any idea why this might be? Thanks in advance.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    How are you getting your reference to
    videoPlayerObj
    ?

    Usually this problem happens because of:
    • A dependence on scripts executing in a particular order
    • A dependence on the game having a certain framerate
    • A dependence on GameObject.Find or similar method returning a particular object when multiple meet the search criteria
    • A dependence on a particular ordering of results in a FindGameObjectsWithTag or similar method.
    None of those things are guaranteed and may differ between platforms or devices.
     
  3. EOMedvis

    EOMedvis

    Joined:
    Feb 19, 2019
    Posts:
    94
    Thanks. I think I figured out what the issue was.

    The Coroutine VideoTimer() does this:


    Code (CSharp):
    1. IEnumerator VideoTimer()
    2.     {
    3.         GameManager.DebugMessage("Video Length: " + videoPlayer.Video_Length + " seconds.");
    4.  
    5.         yield return new WaitForSeconds(videoPlayer.Video_Length);
    6.  
    7.         videoPlayerObj.SetActive(false);
    8.         ShowDownloadScreen();
    9.     }
    on my PC, I was able to load the video and return the time as not 0 before the coroutine was executed, thus the video plays correctly. On the mac, the loading was slower, so the coroutine executes before the video is loaded, thus returning a 0-time, and this the coroutine waits for 0 seconds and disables the video player object as soon as it's enabled.

    adjusting the the coroutine in this way fixed the issue:


    Code (CSharp):
    1. IEnumerator VideoTimer()
    2.     {
    3.         while (!videoPlayer.Video_Is_Playing)
    4.         {
    5.             GameManager.DebugMessage("Loading Video...");
    6.  
    7.             if (videoPlayer.Video_Is_Playing)
    8.             {
    9.                 break;
    10.             }
    11.             yield return new WaitForEndOfFrame();
    12.         }
    13.  
    14.         GameManager.DebugMessage("Video Length: " + videoPlayer.Video_Length + " seconds.");
    15.  
    16.         yield return new WaitForSeconds(videoPlayer.Video_Length);
    17.  
    18.         videoPlayerObj.SetActive(false);
    19.         ShowDownloadScreen();
    20.     }
    not sure if I even need the break in the while loop there, but it works.