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

How do I play popup intro opening animation?

Discussion in 'Scripting' started by tawak3500, May 23, 2016.

  1. tawak3500

    tawak3500

    Joined:
    Oct 28, 2013
    Posts:
    77
    I have a pause popup panel in a different scene that gets loaded on top of the game scene through LoadSceneMode.Additive. I have made custom animations but whenever I try to play them I keep getting null reference error but whenever I test it in the scene with the popup panel it works.

    Also if I made the time scale to 0 when the pause popup opens, how can I still keep playing the popup intro animation?

    Heres what I have. I just took the area the code where I'm referencing the animation.

    Code (CSharp):
    1.   void Start()
    2.     {
    3.         _anim = GameObject.FindGameObjectWithTag("Pause").GetComponent<Animator>();
    4.     }
    5.  
    6.     IEnumerator Intro()
    7.     {
    8.         _anim.SetBool("Intro",true);
    9.         yield return new WaitForSeconds(clip.length);
    10.         _anim.SetBool("Intro",false);
    11.         yield return null;
    12.     }
     
  2. mumbot

    mumbot

    Joined:
    Oct 19, 2012
    Posts:
    86
    Are you sure your tag is the same as the object? Also what is the point in making it a corentine if it only runs once when you pause there should be no problem making it a normal void function.
     
  3. tawak3500

    tawak3500

    Joined:
    Oct 28, 2013
    Posts:
    77
    Yea its tagged. The problem is the object the script is referencing is inside a different scene. How can I access the gameobject in a different scene. I'm using coroutine because for outro animation I think I need it because i want the animation to finish and then close the popup. For intro it doens't really matter your right.
     
  4. mumbot

    mumbot

    Joined:
    Oct 19, 2012
    Posts:
    86
    I do not think its possible to access a gameobject in another scene from the current open scene. You could try something like instantiate the gameobject in your new scene before the animation runs.
     
  5. tawak3500

    tawak3500

    Joined:
    Oct 28, 2013
    Posts:
    77
    Ok thanks. i'll give that a try.
     
  6. tawak3500

    tawak3500

    Joined:
    Oct 28, 2013
    Posts:
    77
    Thanks for the help mumbot. This is what I came up with and it works.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class ButtonManagerPause : ButtonManager
    6. {
    7.     public Animator anim;
    8.  
    9.     void Start ()
    10.     {
    11.         //make it not be affected by Time.Timescale = 0
    12.         anim.updateMode = AnimatorUpdateMode.UnscaledTime;
    13.         anim.SetBool("Intro", true);
    14.     }
    15.        
    16.     public new void UnloadScene(string scene)
    17.     {
    18.         StartCoroutine("Outro", scene);
    19.     }
    20.  
    21.     IEnumerator Outro(string scene)
    22.     {
    23.         Time.timeScale = 1f;
    24.         AudioListener.volume = 1;
    25.         _IsPauseActive = false;
    26.         anim.SetBool("Outro", true);
    27.         //get the animation length of the Outro animation
    28.         yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length);
    29.         SceneManager.UnloadScene(scene);
    30.         yield return null;
    31.     }
    32. }
    33.