Search Unity

Help with Scene Transition

Discussion in 'Scripting' started by Hodoer, Nov 5, 2017.

  1. Hodoer

    Hodoer

    Joined:
    Oct 12, 2017
    Posts:
    6
    Hi all,

    I'm trying to build a 2.5D platformer however I could seem to get the scene transition to work with a fade.
    My script for the empty object with a collider is as follows:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4. using UnityEngine.UI;
    5.  
    6. public class JumpScene1 : MonoBehaviour
    7. {
    8.     public Image black;
    9.     public Animator animate;
    10.  
    11.     public void OnTriggerEnter(Collider c)
    12.     {
    13.         if (c.CompareTag("Player"))
    14.         {
    15.             StartCoroutine(Fading());
    16.         }
    17.     }
    18.    
    19.     IEnumerator Fading()
    20.     {
    21.         animate.SetBool("Fade", true);
    22.         yield return new WaitUntil(() => black.color.a == 1);
    23.         SceneManager.LoadScene("Scene_2_Test");
    24.     }
    25. }
    Please help. Thanks.
     
    Last edited: Nov 6, 2017
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  3. Hodoer

    Hodoer

    Joined:
    Oct 12, 2017
    Posts:
    6
    Thanks for the advice, I've edited my post.
    The problem I encounter is that the Fading Coroutine doesn't trigger at all.
    If I were to only run the SceneManager code in the tag, the scene transition works fine but of course without the fade animation.
    I'm very sure the fade animation works as it also triggers at the start of my scene.
    Hope this is enough information. Thanks.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Not sure what to say.
    Just to double check, are you saying the full coroutine doesn't finish or it doesn't even start?

    And, do you notice the colour alpha value changing at all?
     
  5. Hodoer

    Hodoer

    Joined:
    Oct 12, 2017
    Posts:
    6
    I suppose it doesn't start as the scene transition doesn't occurs when the player collides into the collider space.
    It just passes right through it.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, if it's just a trigger, you can pass right through it, that's normal.
    You didn't answer about the image's alpha value changing at all?

    You can do a Debug.Log (or print) statement at the top of the coroutine to see if it started at all.. (and in your trigger, too, to double check?).

    Try to go step by step through things, check them , and see where it's failing.. :)
     
  7. Hodoer

    Hodoer

    Joined:
    Oct 12, 2017
    Posts:
    6
    Sorry for the late reply.
    As I've also added the same fade animation to the beginning of the level, I can see the alpha value changed here.
    However, it doesn't occur in the trigger.
    I've added in the Debug,Log as you suggested:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4. using UnityEngine.UI;
    5.  
    6. public class JumpScene1 : MonoBehaviour
    7. {
    8.     public Image black;
    9.     public Animator animate;
    10.  
    11.     public void OnTriggerEnter(Collider c)
    12.     {
    13.         Debug.Log("Collision Detected");
    14.         if (c.CompareTag("Player"))
    15.         {
    16.             Debug.Log("Coroutine Started");
    17.             StartCoroutine(Fading());
    18.  
    19.             //SceneManager.LoadScene("Scene_2_Test");
    20.         }
    21.     }
    22.    
    23.     IEnumerator Fading()
    24.     {
    25.         animate.SetBool("Fade", true);
    26.         yield return new WaitUntil(() => black.color.a == 1);
    27.         SceneManager.LoadScene("Scene_2_Test");
    28.     }
    29. }
    It seems that both the trigger and the coroutine have be activated but neither the fade nor the scene change occurred.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hm, not sure what to say. Something is wrong with the animation, or the object is deactivated maybe ?
     
  9. Hodoer

    Hodoer

    Joined:
    Oct 12, 2017
    Posts:
    6
    I finally found a way to make the fade transition work with a few tweaks to the script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AnimationOnTrigger : MonoBehaviour
    6. {
    7.     public string scene;
    8.     public Color loadColor = Color.black;
    9.  
    10.     public void OnTriggerEnter(Collider c)
    11.     {
    12.         if (c.CompareTag("Player"))
    13.         {
    14.             Initiate.Fade(scene, loadColor, 0.5f);
    15.         }
    16.     }
    17. }
    I'm posting this here for anyone who may encounter the same problem in the future.