Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem with upgrading a code from unity 5.1 to 5.3

Discussion in 'Scripting' started by Eths, Dec 10, 2015.

  1. Eths

    Eths

    Joined:
    Mar 5, 2015
    Posts:
    184
    Alright so here is the old code (unity 5.1's code)

    Code (CSharp):
    1.     using UnityEngine;
    2.     using UnityEngine.UI;
    3.     [AddComponentMenu("Silver Bullet/Videos and Cutscenes/Play Video")]
    4.    
    5.     public class IntroVideo : MonoBehaviour
    6.     {
    7.         ///Variables
    8.         public MovieTexture[] Videos;
    9.         public AudioClip[] Audios;
    10.         public AudioSource[] Audiosources;
    11.         public bool[] Skipable;
    12.         public int VideosNumber;
    13.         private float Timer;
    14.         private bool VideoIsPlaying = false;
    15.         public int CurrentVideo = 0;
    16.         public GameObject GameObject;
    17.         public Canvas VideoCanvas;
    18.         public RawImage VideoRender;
    19.    
    20.         void Awake () {
    21.             Timer = Videos[0].duration;
    22.             Audiosources[0].Stop();
    23.      
    24.         }
    25.    
    26.    
    27.         void OnGUI(){
    28.    
    29.         }
    30.         void PlayVideo(){
    31.             if (CurrentVideo == 0) {
    32.                 if (Videos[0].isReadyToPlay == true && VideoIsPlaying == false) {
    33.                     Audiosources[0].Stop();
    34.                     VideoIsPlaying = true;
    35.                     Videos[0].Play();
    36.                     Audiosources[0].clip = Audios[0];
    37.                     Audiosources[0].Play();
    38.                 }
    39.             } else if (CurrentVideo  < VideosNumber || CurrentVideo == VideosNumber) {
    40.                 if (Videos[CurrentVideo].isReadyToPlay == true && VideoIsPlaying == false) {
    41.                     Videos[CurrentVideo-1].Stop();
    42.                     VideoIsPlaying = true;
    43.                     Audiosources[CurrentVideo-1].Stop();
    44.                     Timer = Videos[CurrentVideo].duration;
    45.                     Videos[CurrentVideo].Play();
    46.                     Audiosources[CurrentVideo].clip = Audios[CurrentVideo];
    47.                     Audiosources[CurrentVideo].Play();
    48.                     if(!Audiosources[CurrentVideo].isPlaying)
    49.                     {
    50.                         Audiosources[CurrentVideo].clip = Audios[CurrentVideo];
    51.                         Audiosources[CurrentVideo].Play();
    52.                     }
    53.                 }
    54.             }
    55.         }
    56.         void Update(){
    57.             Cursor.visible = false;
    58.    
    59.             if ((CurrentVideo == VideosNumber) && !Application.isLoadingLevel)
    60.             {
    61.                 VideoRender.texture = Videos[CurrentVideo];
    62.                 if (VideoIsPlaying == false)
    63.                 {
    64.                     PlayVideo();
    65.                 }
    66.             }
    67.             if ((CurrentVideo < VideosNumber) && !Application.isLoadingLevel)
    68.             {
    69.                 VideoRender.texture = Videos[CurrentVideo];
    70.                 if (VideoIsPlaying == false)
    71.                 {
    72.                     PlayVideo();
    73.                 }
    74.             }
    75.             if (!Application.isLoadingLevel) {
    76.                 Timer -= Time.deltaTime;
    77.                 if (Input.GetKeyDown ("space")) {
    78.                     if ((CurrentVideo < VideosNumber) && Skipable[CurrentVideo]) {
    79.                         CurrentVideo ++;
    80.                         VideoIsPlaying = false;
    81.                         PlayVideo ();
    82.                     } else if ((CurrentVideo == VideosNumber) && Skipable[CurrentVideo]) {
    83.                         GameObject LoadingObject = GameObject.FindGameObjectWithTag("LoadingController");
    84.                         LoadingScene Loading = (LoadingScene)LoadingObject.GetComponent(typeof(LoadingScene));
    85.                         PlayerScreen.StartLoadingScreen();
    86.                         Audiosources[CurrentVideo].Stop();
    87.                         Videos[CurrentVideo].Stop();
    88.                         VideoCanvas.gameObject.SetActive(false);
    89.                         Loading.LoadingAsync = Application.LoadLevelAsync (1);
    90.                     }
    91.                 }
    92.                 if (ControlsAndConfigure.GetJoySticksConnected() > 0 && Input.GetKeyDown (KeyCode.JoystickButton2)) {
    93.                     if ((CurrentVideo < VideosNumber) && Skipable[CurrentVideo]) {
    94.                         CurrentVideo ++;
    95.                         VideoIsPlaying = false;
    96.                         PlayVideo ();
    97.                     } else if ((CurrentVideo == VideosNumber) && Skipable[CurrentVideo]) {
    98.                         GameObject LoadingObject = GameObject.FindGameObjectWithTag("LoadingController");
    99.                         LoadingScene Loading = (LoadingScene)LoadingObject.GetComponent(typeof(LoadingScene));
    100.                         PlayerScreen.StartLoadingScreen();
    101.                         Audiosources[CurrentVideo].Stop();
    102.                         Videos[CurrentVideo].Stop();
    103.                         VideoCanvas.gameObject.SetActive(false);
    104.                         Loading.LoadingAsync = Application.LoadLevelAsync(1);
    105.                     }
    106.                 }
    107.                 if (((Timer < 0 || Timer == 0) && (CurrentVideo < VideosNumber))) {
    108.                     if (VideoIsPlaying == true) {
    109.                         CurrentVideo ++;
    110.                         VideoIsPlaying = false;
    111.                         PlayVideo ();
    112.                     }
    113.                 }
    114.                 if ((Timer < 0 || Timer == 0) && (CurrentVideo == VideosNumber)) {
    115.                     GameObject LoadingObject = GameObject.FindGameObjectWithTag("LoadingController");
    116.                     LoadingScene Loading = (LoadingScene)LoadingObject.GetComponent(typeof(LoadingScene));
    117.                     PlayerScreen.StartLoadingScreen();
    118.                     Audiosources[CurrentVideo-1].Stop();
    119.                     Videos[CurrentVideo-1].Stop();
    120.                     VideoCanvas.gameObject.SetActive(false);
    121.                     Loading.LoadingAsync = Application.LoadLevelAsync(1);
    122.                 }
    123.             }
    124.         }
    125.     }
    126.  
    127.  
    I tried to use Unity's new scene management But I can't find a proper way to do so :/ Can anyone help me upgrading this code please?
     
  2. mikeymike

    mikeymike

    Joined:
    Oct 21, 2014
    Posts:
    35
    you have to use the namespace for scene manager.
    Code (csharp):
    1.  using UnityEngine.SceneManagement;
    then you can use SceneManager.LoadSceneAsync(1), etc etc
     
  3. puppeteer

    puppeteer

    Joined:
    Sep 15, 2010
    Posts:
    1,282
    Any idea why it doesn't update the API automatically like it does with other code elements? ( rigidbody --> GetComponent<RigidBody>(), etc )
     
  4. mikeymike

    mikeymike

    Joined:
    Oct 21, 2014
    Posts:
    35
    no idea, you can still use the old application.loadLevel stuff, so maybe that has something to do with it.
     
  5. puppeteer

    puppeteer

    Joined:
    Sep 15, 2010
    Posts:
    1,282
    You're correct, it's only a warning. The project works just as intended, but if they set it as obsolete then someday it will be removed and we shouldn't use it anymore. Maybe they forgot to include it, this list here shows what they *don't* update automatically, but the Application.LoadLevel stuff is not on that list ( See the list of limitations right at the bottom):
    http://docs.unity3d.com/Manual/APIUpdater.html

    Wish we could get some official answer on this. Now I'm scrambling to disable warnings for some of the packages I sell on the Unity Asset Store so that people don't get a ton of warnings when they upgrade to 5.3, but it's only viable for C# and there's no way to do it in JS.

    btw, here's the code to make the warnings disappear in C#. Put this on at the top of your script which has something related to Application.LoadLevel:

    #pragma warning disable 618
     
    mikeymike likes this.
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You shouldn't disable the warnings. You should use compiler directives to use SceneManager in Unity 5.3 and Application in all other versions.
     
    BenZed likes this.
  7. puppeteer

    puppeteer

    Joined:
    Sep 15, 2010
    Posts:
    1,282
    I was hoping to have it as a temporary solution before finding a good solution to work with. This can't be only my problem, every single package on the asset store that loads level and has a version before 5.3 has this potential problem for buyers who upgrade. I may be missing something here, but I think it affects alot of people, am I wrong?

    From reading about compiler directives, it seems like they have platform definitions only up to a major version of 5. But the problem starts with 5.3, whereas 5.2.3 and below don't contain Unity.SceneManagement at all.

    Is there a way to differentiate between 5.2.3 and 5.3?
     
  8. Eths

    Eths

    Joined:
    Mar 5, 2015
    Posts:
    184
    I tried that already mikeymike. it still doesn't work as intended. also my scene is just a mess, there is more than 50 scenes inside my scene so I don't know what to do. unity 5.3 is full of bugs..
     
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    This doesn't work?

    Code (csharp):
    1.  
    2. #if UNITY_5_3
    3. SceneManager.......
    4. #else
    5. Application......
    6. #endif
    7.  
     
  10. puppeteer

    puppeteer

    Joined:
    Sep 15, 2010
    Posts:
    1,282
    @KelsoMRK:
    I just tried it out, upgrading from Unity 4 to 5.2.3 to 5.3. It works like a charm! It was confusing for me as I thought it only covered the numbered version specifically, didn't know I could specify smaller versions.

    Here's a sample code based on Kelso's suggestion:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. #if UNITY_5_3
    4. import UnityEngine.SceneManagement;
    5. #endif
    6.  
    7. //@script ExecuteInEditMode
    8.  
    9. var positionAndSize:Rect = Rect( 100, 100, 600, 400);
    10.  
    11. //Tha actual text description to be displayed
    12. var description:String;
    13.  
    14. var guiSkin:GUISkin;
    15.  
    16. function OnGUI()
    17. {
    18.     GUI.skin = guiSkin;
    19.  
    20.     GUI.Label( positionAndSize, description);
    21.  
    22.     //A button which goes bacck to the start menu
    23.     if ( GUI.Button( Rect( (Screen.width - 300) * 0.5, Screen.height - 60, 300, 50), "Back To Menu") )
    24.     {
    25.         if ( GetComponent.<AudioSource>() ) GetComponent.<AudioSource>().Play();
    26.      
    27.         #if UNITY_5_3
    28.             SceneManager.LoadScene("StartMenu");
    29.         #else
    30.             Application.LoadLevel("StartMenu");
    31.         #endif
    32.      
    33.     }
    34.  
    35.     GUI.Label( positionAndSize, "" );
    36. }
     
  11. Eths

    Eths

    Joined:
    Mar 5, 2015
    Posts:
    184
    Okay guys here is my new code :/ I expected it to work but it didn't

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.SceneManagement;
    4. using System.Collections;
    5.  
    6. [AddComponentMenu("Silver Bullet/Videos and Cutscenes/Play Video")]
    7.  
    8. public class IntroVideo : MonoBehaviour
    9. {
    10.     ///Variables
    11.     public MovieTexture[] Videos;
    12.     public AudioClip[] Audios;
    13.     public AudioSource[] Audiosources;
    14.     public bool[] Skipable;
    15.     public int VideosNumber;
    16.     private float Timer;
    17.     private bool VideoIsPlaying = false;
    18.     public int CurrentVideo = 0;
    19.     public GameObject GameObject;
    20.     public Canvas VideoCanvas;
    21.     public RawImage VideoRender;
    22.  
    23.     private bool VideoFinished;
    24.  
    25.     void Awake () {
    26.        
    27.         GameObject LoadingObject = GameObject.FindGameObjectWithTag("LoadingController");
    28.         LoadingScene Loading = (LoadingScene)LoadingObject.GetComponent(typeof(LoadingScene));
    29.  
    30.         Timer = Videos[0].duration;
    31.         Audiosources[0].Stop();
    32.  
    33.  
    34.         Loading.LoadingAsync = null;
    35.         VideoFinished = false;
    36.     }
    37.     void PlayVideo(){
    38.         if (CurrentVideo == 0) {
    39.             if (Videos[0].isReadyToPlay == true && VideoIsPlaying == false) {
    40.                 Audiosources[0].Stop();
    41.                 VideoIsPlaying = true;
    42.                 Videos[0].Play();
    43.                 Audiosources[0].clip = Audios[0];
    44.                 Audiosources[0].Play();
    45.             }
    46.         } else if (CurrentVideo  < VideosNumber || CurrentVideo == VideosNumber) {
    47.             if (Videos[CurrentVideo].isReadyToPlay == true && VideoIsPlaying == false) {
    48.                 Videos[CurrentVideo-1].Stop();
    49.                 VideoIsPlaying = true;
    50.                 Audiosources[CurrentVideo-1].Stop();
    51.                 Timer = Videos[CurrentVideo].duration;
    52.                 Videos[CurrentVideo].Play();
    53.                 Audiosources[CurrentVideo].clip = Audios[CurrentVideo];
    54.                 Audiosources[CurrentVideo].Play();
    55.                 if(!Audiosources[CurrentVideo].isPlaying)
    56.                 {
    57.                     Audiosources[CurrentVideo].clip = Audios[CurrentVideo];
    58.                     Audiosources[CurrentVideo].Play();
    59.                 }
    60.             }
    61.         }
    62.     }
    63.     void Update()
    64.     {
    65.         Cursor.visible = false;
    66.  
    67.         if ((CurrentVideo == VideosNumber) && !VideoFinished)
    68.         {
    69.             VideoRender.texture = Videos[CurrentVideo];
    70.             if (VideoIsPlaying == false)
    71.             {
    72.                 PlayVideo();
    73.             }
    74.         }
    75.         if ((CurrentVideo < VideosNumber) && !VideoFinished)
    76.         {
    77.             VideoRender.texture = Videos[CurrentVideo];
    78.             if (VideoIsPlaying == false)
    79.             {
    80.                 PlayVideo();
    81.             }
    82.         }
    83.         if (VideoFinished == false) {
    84.             Timer -= Time.deltaTime;
    85.             if (Input.GetKeyDown ("space")) {
    86.                 if ((CurrentVideo < VideosNumber) && Skipable[CurrentVideo] && !VideoFinished) {
    87.                     CurrentVideo ++;
    88.                     VideoIsPlaying = false;
    89.                     PlayVideo ();
    90.                 } else if ((CurrentVideo == VideosNumber) && Skipable[CurrentVideo] && !VideoFinished) {
    91.                     PlayerScreen.StartLoadingScreen();
    92.                     Audiosources[CurrentVideo].Stop();
    93.                     Videos[CurrentVideo].Stop();
    94.                     VideoCanvas.gameObject.SetActive(false);
    95.                     VideoFinished = true;
    96.                     StartCoroutine (LoadNextScene ());
    97.                 }
    98.             }
    99.             if (ControlsAndConfigure.GetJoySticksConnected() > 0 && Input.GetKeyDown (KeyCode.JoystickButton2)) {
    100.                 if ((CurrentVideo < VideosNumber) && Skipable[CurrentVideo] && !VideoFinished) {
    101.                     CurrentVideo ++;
    102.                     VideoIsPlaying = false;
    103.                     PlayVideo ();
    104.                 } else if ((CurrentVideo == VideosNumber) && Skipable[CurrentVideo] && !VideoFinished) {
    105.                     PlayerScreen.StartLoadingScreen();
    106.                     Audiosources[CurrentVideo].Stop();
    107.                     Videos[CurrentVideo].Stop();
    108.                     VideoCanvas.gameObject.SetActive(false);
    109.                     VideoFinished = true;
    110.                     StartCoroutine (LoadNextScene ());
    111.                 }
    112.             }
    113.             if (((Timer < 0 || Timer == 0) && (CurrentVideo < VideosNumber)) && !VideoFinished) {
    114.                 if (VideoIsPlaying == true) {
    115.                     CurrentVideo ++;
    116.                     VideoIsPlaying = false;
    117.                     PlayVideo ();
    118.                 }
    119.             }
    120.             if ((Timer < 0 || Timer == 0) && (CurrentVideo == VideosNumber) && !VideoFinished) {
    121.                 PlayerScreen.StartLoadingScreen();
    122.                 Audiosources[CurrentVideo-1].Stop();
    123.                 Videos[CurrentVideo-1].Stop();
    124.                 VideoCanvas.gameObject.SetActive(false);
    125.                 VideoFinished = true;
    126.                 StartCoroutine (LoadNextScene ());
    127.             }
    128.         }
    129.  
    130.         GameObject LoadingObject = GameObject.FindGameObjectWithTag("LoadingController");
    131.         LoadingScene Loading = (LoadingScene)LoadingObject.GetComponent(typeof(LoadingScene));
    132.  
    133.         if (VideoFinished && Loading.LoadingCompleted) {
    134.             SceneManager.LoadScene (1, LoadSceneMode.Single);
    135.         }
    136.     }
    137.     IEnumerator LoadNextScene()
    138.     {
    139.         GameObject LoadingObject = GameObject.FindGameObjectWithTag("LoadingController");
    140.         LoadingScene Loading = (LoadingScene)LoadingObject.GetComponent(typeof(LoadingScene));
    141.  
    142.         Loading.LoadingAsync = SceneManager.LoadSceneAsync(1);
    143.         yield return Loading.LoadingAsync;
    144.         Loading.LoadingCompleted = true;
    145.     }
    146. }
    Here is a photo inside the scene after I click play :/


    as you can see there is a lot of Scenes Called "Awake" while I only wanted one ..
     
  12. Eths

    Eths

    Joined:
    Mar 5, 2015
    Posts:
    184
    but my project is already upgraded to 5.3 :O