access animator

Hi,
how can you access in scene A the animator of one gameobject which is in scene B?

this code works only when both gameobjects are in the same sceme , but my gameobject is in scene B. how to fix it?

 public GameObject otherObject;

Animator otherAnimator;

void Awake ()
{
     otherAnimator = otherObject.GetComponent<Animator> ();

}

One way is to use a shared object such as a GameManager to facilitate communications between scenes. When entities come up they can register themselves with the GameManager so other things can find them.

Alternately you can look for things via tag, or else via component, in this case some component that uniquely marks the GameObject where the animator is.

  public static GameManager3D Instance;

    public Animator doorAnim;


    private void Awake()
    {

        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }

MissingReferenceException: The object of type ‘Animator’ has been destroyed but you are still trying to access it.

 private Animator anim;


    private void Start()
    {
        instance = GameObject.Find("GameManager").GetComponent<GameManager>();
        anim = GameManager3D.Instance.doorAnim.GetComponent<Animator>();
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Player2D"))
        {
            if (instance.coinCount > 1)
            {
                // all coins are collected
                // house door will be opened.
                Debug.Log(anim);
                // play door animation
                anim.SetBool("isOpen", true);

                collectedAllCoinsPanel.SetActive(true);


            }

any idea why animator is destroyed? and how to fix it?

when i change the scene , actually this singleton cannot keep the gameobject with it rather only the singleton gameobject is persistence between the scene change.
how to fix it? how to keep also the referenced gameobject in the singleton class?

 private void Awake()
    {
        Debug.Log("awake");

        this.GetComponentInChildren<Camera>().enabled = true;
        this.GetComponentInChildren<AudioListener>().enabled = true;

        rb.isKinematic = false;
        rb.useGravity = true;

        if (player3DInstance != null)
        {
            Destroy(gameObject);
            return;
        }
        else
        {
            player3DInstance = this;
        }

        DontDestroyOnLoad(gameObject);
    }

this runs each time however i dont know why these
this.GetComponentInChildren().enabled = true;
this.GetComponentInChildren().enabled = true;

rb.isKinematic = false;
rb.useGravity = true;

are not running

any idea please?

Any GameObject you want kept from one scene to the next has to either be marked DontDestroyOnLoad, or parented to something that is. Nothing special about that with Singletons. That’s just Unity.

1 Like

any idea why this is not working please?

i fixed it, thanks but now there is another problem as i wrote about just above. do you know what is wrong there?

Generally Awake() should only be used to do things to objects in your same class instance.

The reason: very likely the Camera, AudioListener and Rigidbody haven’t had their Awake() yet.

Generally if you want to be touching other things, do it in Start().

Here is some timing diagram help:

still no answer?

What part of my answer did you not understand?

1 Like

i appreciate your help however still when i load the scene and come back to that scene, camera component and audio listener dont get active again.

may you help with that please?
here is the code :

    private void Start()
    {
        instance = GameObject.Find("GameManager2D").GetComponent<GameManager>();
        player3D = GameObject.Find("Player").GetComponent<Player3D>();
        anim = HouseDoor.Instance.GetComponent<Animator>();

        player3D.GetComponent<Rigidbody>().isKinematic = true;
        player3D.GetComponent<Rigidbody>().useGravity = false;

        player3D.GetComponentInChildren<Camera>().enabled = false;
        player3D.GetComponentInChildren<AudioListener>().enabled = false;
    }

this code works in sceme B but back to scene A in Player3D script the same code dont work.

whats wrong?

This is unlikely to be code. It’s more likely to be how you set stuff up. If you change scenes, all other things being equal, you need to get new references to the new things in the new scene.

If you keep things from the previous scene (with DontDestroyOnLoad), then you need to NOT have them in the next scene, otherwise you’ll have double cameras, double audio listeners, etc.

1 Like