follow mouse.

Hello everyone,

i create a object from a button, but i get an error when i try to attach to my mousescript.
(nullReferenceException)Object reference not set to an instance of an object.

and i know what its means. But are there other ways to attach my object to the mouse?

Code of the button

using UnityEngine;
using System.Collections;

public class OnTheClick : MonoBehaviour {
    public GameObject tree;
    tileMapMouseScript mouseScript;
    public void objClick(bool click)
    {
        if (click)
        {
            GameObject treeObj=Instantiate(tree) as GameObject;
            mouseScript.GetComponent<tileMapMouseScript>().SelectionObject=treeObj;
        }
    }
}

the MouseScript

using UnityEngine;
using System.Collections;
[RequireComponent(typeof(tilemap))]
public class tileMapMouseScript : MonoBehaviour {
    tilemap _tileMap;
    Vector3 tileCor;
    public GameObject SelectionObject;
    void Start()
    {
        _tileMap = GetComponent<tilemap> ();
    }
    // Update is called once per frame
    void Update () {
        Ray ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);
        RaycastHit hitInfo;
        if (collider.Raycast (ray, out hitInfo, Mathf.Infinity)) {
            int x =Mathf.FloorToInt( hitInfo.point.x /_tileMap.tileSIze);
            int z =Mathf.FloorToInt( hitInfo.point.z /_tileMap.tileSIze);
            ///transform.worldToLocalMatrix;
            tileCor.x=x;
            tileCor.z=z;
            tileCor.y=+1;
            if(SelectionObject!=null)
            {
                SelectionObject.transform.position=tileCor;
            }

                } else {
                }
    }
}

Never post something like this without giving the line that the error occurred on. It just wastes everyone’s time, and give the full error message.