- Home /
Object not following mouse
Im trying to make the object follow my cursor. Im working in a 2d shooting game. There is no error before playing the scene but after playing it gives me this error: "Object reference not set into an instance of an object"
This is my code for the object to follw my cursor: public float offset;
public void Update()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);
}
I am following a tutorial by blackthornprod: https://www.youtube.com/watch?v=hk6cUanSfXQ
when I downloaded and tried this code to the weapon of his character. The script works fine. But when I try to use the script in my own game it is not working. I suspect that it is because of a setting in a Object setting.
Knowledge about Unity: Beginner.
Answer by madruytb · Oct 07, 2018 at 03:32 AM
Unity is telling you that some object that your script is referencing, doesnt exists, thats the only thing I can find to help you, as I dont have more info in your game/script, hope it helped you a little.
Your answer
![](https://koobas.hobune.stream/wayback/20220612175901im_/https://answers.unity.com/themes/thub/images/avi.jpg)