Make game object follow cursor - Help

Hello,

I’m a total newbie, trying to make a character summon an object, and for that object to follow mouse cursor position when the mouse button is pressed, but while the muse button is pressed i get a continuous error “NullReferenceexception: object reference not set to an instance of an object”

This is my script for the character:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControler : MonoBehaviour
{
    Rigidbody2D rigbody;

    public GameObject FlamePrefab;

    public Transform nenflameObj;


    float horizontal;
    float vertical;

    public float runSpeed = 5.0f;

    // Start is called before the first frame update
    void Start()
    {
        rigbody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    public void Update()
    {
        horizontal = Input.GetAxisRaw("Horizontal");
        vertical = Input.GetAxisRaw("Vertical");

        if (Input.GetKeyDown(KeyCode.C))
        {
            Instantiate();
        }

    }
    public void Instantiate()
    {
        GameObject nenflameObj = Instantiate(FlamePrefab);

    }
private void FixedUpdate()
    {
        rigbody.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);
    }
}

And this is the script for the object

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NenFlame : MonoBehaviour
{

    private Vector3 mousePosition;
    private Rigidbody2D rb;
    private Vector2 direction;
    private float moveSpeed = 100f;

    // Use this for initialization
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    public void Update()
    {
        if (Input.GetMouseButton(0))
        {
            mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            direction = (mousePosition - transform.position).normalized;
            rb.velocity = new Vector2(direction.x * moveSpeed, direction.y * moveSpeed);
        }
        else
        {
            rb.velocity = Vector2.zero;
        }
    }
}

I got stuck on this for a while.

Thanks in advance

More details needed. If you click the error in the Console it should give you more, like which script and line number the error happened on (I think it does this for all errors but possibly not?)

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above.

1 Like

This script I made might help(also, it’s 2d):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class followobject : MonoBehaviour
{
public GameObject Object;//this needs to be set in inspector
private Vector3 moveto;

// Update is called once per frame
void Update(){
moveto=new Vector3(Input.mousePosition.x,Input.mousePosition.y,0);

Object.transform.position=moveto;
}
}

Just stick it on absolutely anything