Object reference not set to an instance of an Object C#

Hi guys! I need help with this thing that driving me nuts.

There are two scripts, one attached to ballPrefeb called “clickToDestroy.cs” and “activateAnimation.cs” that is attached to character who is going to be animated. The thing that I want is that when the ball is destroyed (clicked by OnMouseDown) it reads the position where has been destroyed and use that in “activateAnimation.cs” script for “triggering” particular animation based on that position. Problem (for now) is that I’m getting an error “NullReferenceException: Object reference not set to an instance of an object” in line 28. I assume that I’m getting this error because script doesn’t know for what object are those positions. Please help me with this one! Here are the scripts.

This is “clickToDestroy.cs”:

using UnityEngine;
using System.Collections;

public class clickToDestroy : MonoBehaviour {

    public GameObject splashReference;
    public GameObject effectReference;
    public int scoreValue;
    private GameController gameController;
    public Vector3 _lastPosition;

  

    void Start()
    {


        GameObject gameControllerObject = GameObject.FindWithTag("GameController");
        if (gameControllerObject != null)
        {
            gameController = gameControllerObject.GetComponent<GameController>();
        }
        if (gameController == null)
        {
            Debug.Log("Cannot find 'GameController' script");
        }
    }

    public void OnMouseDown ()
    {
        gameController.AddScore(scoreValue);
        _lastPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
        Destroy(gameObject);
        Instantiate(splashReference, transform.position, transform.rotation);
        Instantiate(effectReference, transform.position, transform.rotation);

                Debug.Log(_lastPosition);

    }
        }

And this is “activateAnimation.cs”:

using UnityEngine;
using System.Collections;

public class activateAnimation : MonoBehaviour {

    public Animator anim;


   public  clickToDestroy _clickToDestroy;


  //  GameObject yourObjects;
 //   private GameObject theCollision;

    public void Start()
    {
        anim = GetComponent<Animator>();
        _clickToDestroy = gameObject.GetComponent<clickToDestroy>();


    }


    public void Update()

    {

        if (_clickToDestroy._lastPosition.x >= 10 && _clickToDestroy._lastPosition.x <= 80)
            {
                anim.SetTrigger("levi");
            }
        }

    }

As far as I can see both your scripts are attached to the ball that you are destroying. When a gameobject is destroyed, all scripts attached to it are destroyed as well. So if you want a script to outlive a gameobject you should attach it to another gameobject (for example an empty gameobject called Managers or something).

Thank you for your answer @Tyche10 !

I have GameController script and from there balls are getting generated as clones. So those clones are destroying script as well, even if i attached script to ballPrefeb? And if I try solution to make new gameobject, should I attach just “clickToDestroy.cs” to it?