Search Unity

Null Reference Exception : The Playable Graph is null

Discussion in 'Timeline' started by rushk1, Jan 22, 2019.

  1. rushk1

    rushk1

    Joined:
    Sep 24, 2017
    Posts:
    39
    In the Class Movie , I am pausing a timeline when its gameobject is disabled
    Code (CSharp):
    1. public void PauseMovie()
    2.     {
    3.         myPlayableDirector.playableGraph.GetRootPlayable(0).SetSpeed(0);
    4.     }
    and then Resuming it when its gameobject is enabled

    Code (CSharp):
    1. public void ResumeMovie()
    2.     {
    3.        myPlayableDirector.playableGraph.GetRootPlayable(0).SetSpeed(1);
    4.     }
    Play on awake is false.

    when I get this error :
    NullReferenceException: The PlayableGraph is null.

    UnityEngine.Playables.PlayableGraph.GetRootPlayableInternal (System.Int32 index) <0x3f964f10 + 0x00072> in <d616be05755d41a089810b5c14e4dd50>:0

    UnityEngine.Playables.PlayableGraph.GetRootPlayable (System.Int32 index) (at C:/buildslave/unity/build/Runtime/Export/Director/PlayableGraph.bindings.cs:29)

    Movie.ResumeMovie () (at Assets/Final/Scripts/Movie.cs:86)


    What am I doing wrong?
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    The graph will be null if the director hasn't started playing, or is stopped. You can for a valid graph using:

    if (myPlayableDirector.playableGraph.IsValid()) ....
     
    AnaLuyza and ThePilgrim like this.
  3. rushk1

    rushk1

    Joined:
    Sep 24, 2017
    Posts:
    39
    This works :
    GameObject to which Playable Director is attached is enabled
    PlayableDirector.Play() is called
    PlayableDirector is Paused using GetRootPlayable(0).SetSpeed(0)
    PlayableDirector is Resumed using GetRootPlayable(0).SetSpeed(1)

    This Does Not Work:
    GameObject to which Playable Director is attached
    PlayableDirector.Play() is called
    PlayableDirector is Paused using GetRootPlayable(0).SetSpeed(0)
    GameObject to which Playable Director is attached is disabled
    GameObject to which Playable Director is attached is enabled
    PlayableDirector is Resumed using GetRootPlayable(0).SetSpeed(1) => This gives error
     
  4. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    When you disable the gameObject it stops playing, and deletes the playableGraph. Without PlayOnAwake checked, there is no playable graph created when it's re-enabled, hence the null reference.
     
    rushk1 likes this.
  5. tweedie

    tweedie

    Joined:
    Apr 24, 2013
    Posts:
    311

    Can we get the Null Ref error changed, as the graph is a struct? That error does not make it clear we should be using IsValid() in place of a null check
     
    INeatFreak likes this.
  6. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    The graph is a struct, but is really just a reference to a native object. The null ref error indicates the native object is null, which is the same thing IsValid is checking.
     
  7. CPlusSharp22

    CPlusSharp22

    Joined:
    Dec 1, 2012
    Posts:
    111
    Is there a way around this limitation? I don't want the playableGraph to get deleted. I intent to re-enable it later and would like it to resume.
    It seems like the AnimationClips are getting destroyed from the graph when I disable the gameobject. I want to keep these.
     
  8. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    Unfortunately no, there isn't. You would have to capture the time of the playable director (either from OnDisable or Update) and set it on OnEnable to have the continue behaviour.

    I don't see why the animation clips would get destroyed though -- they still referenced by the timeline asset (not the graph), which should stay loaded until the playableDirector is destroyed.
     
  9. ryanrlmg

    ryanrlmg

    Joined:
    Jun 30, 2020
    Posts:
    1

    Is this something I should be worrying about down the road? Nothing seems to be breaking but when my timeline is finished it's just returning null which makes sense.
     
  10. m00nandvenus46

    m00nandvenus46

    Joined:
    Nov 18, 2020
    Posts:
    36
    I have only one animation in project - in one compilation file. Translator gives me this error:
    "NullReferenceException: The PlayableGraph is null." without indicating what is the line of the error.
    I use this code from documentation:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Playables;
    5. using UnityEngine.Animations;
    6.  
    7. [RequireComponent(typeof(Animator))]
    8. public class Test : MonoBehaviour
    9. {
    10.     [SerializeField] public AnimationClip clip;
    11.     [SerializeField] PlayableGraph playableGraph;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {    
    16.         StartCoroutine(MyAnimation());
    17.     }
    18.  
    19.     private IEnumerator MyAnimation() {
    20.  
    21.         playableGraph = PlayableGraph.Create();
    22.  
    23.         playableGraph.SetTimeUpdateMode(DirectorUpdateMode.GameTime);
    24.  
    25.         var playableOutput = AnimationPlayableOutput.Create(playableGraph, "Animation", GetComponent<Animator>());
    26.  
    27.         // Wrap the clip in a playable
    28.  
    29.         var clipPlayable = AnimationClipPlayable.Create(playableGraph, clip);
    30.  
    31.         // Connect the Playable to an output
    32.  
    33.         playableOutput.SetSourcePlayable(clipPlayable);
    34.  
    35.         // Plays the Graph.
    36.  
    37.         playableGraph.Play();
    38.         yield return new WaitForSeconds(1.5f);
    39.  
    40.         Destroy(this.gameObject);
    41.     }
    42.  
    43.     void OnDisable()
    44.     {
    45.  
    46.         // Destroys all Playables and PlayableOutputs created by the graph.
    47.  
    48.         playableGraph.Destroy();
    49.     }
    50. }
    How can I fix it? IsValid doesn't help. (What should I do in other cases when I have for example memory leak - (index out of array bounds) and I don't know the exact line?). How the PlayOnAwake flag is related to animation? -This flag is on the audio source. I could play the sound with PlayOneShot() but it is not an animation. V2019.4.15.f1
     
    Last edited: Dec 31, 2020
  11. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    If there is no line number attached, then the error could be occurring while the graph is playing (i.e. there is no managed code causing it).

    I would guess something else is destroying the playable graph created by that graph, but I'm not sure what could do that. All graphs are destroyed when a domain reload occurs (i.e. scripts are reloaded). Is there anything like that which happens during the 1.5s the coroutine is yielded for?