object reference not set to an instance of an object

THANKS IN ADVANCE :slight_smile:

I’m pretty new to Unity. I am working on a 2D RPG. I am trying to make it so that on pressing the Alpha1 key a spell (Freeze) is instantiated in front of the player (Anna). Ive played around with the script but nothing seems to work keep getting the same error:

NullReferenceException: Object reference not set to an instance of an object. Freeze.Update () (at Asset/Freeze.cs:28)

Using CSharp
Here’s the script:
using UnityEngine;
using System.Collections;

public class Freeze : MonoBehaviour {

	public HurtEnemy HurtEnemy;
	private CircleCollider2D circcoll;
	private Animator anmtr;
	public Transform user;
	public AnnaController AnnaController;
	public GameObject ASC;

	// Use this for initialization
	void Start () {

		HurtEnemy = GetComponent<HurtEnemy> ();
		AnnaController = GetComponent<AnnaController> ();
		circcoll = GetComponent<CircleCollider2D> ();
		anmtr = GetComponent<Animator> ();
	
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKeyDown (KeyCode.Alpha1)) 
		{
			Debug.Log ("LOL");
			Instantiate (ASC, new Vector2(user.position.x, user.position.y) + new Vector2(AnnaController.MvmntVctr.x, AnnaController.MvmntVctr.y), Quaternion.Euler(0,0,0));
		}
}
}

Yet again, thanks in advance please let me know if there is a detail needed for solving this.:smiley:
The Debug.Log was just to check if i had made an error with my GetKeyDown

Instantiate (ASC, new Vector2(user.position.x, user.position.y) + new Vector2(AnnaController.MvmntVctr.x, AnnaController.MvmntVctr.y), Quaternion.Euler(0,0,0));

So your problem in on this line, look at everything that you are using the dot operator on the . symbol.

  • user
  • AnnaController
  • AnnaController.MvmnVctr

AnnaController.MvmntVctr

One of these is null, as in it has not been set. So below are the possibilities:

  • I cannot see user being set, I see it is public so I assume you’re drag dropping from somewhere else? Check that it’s been set properly in the scene.

  • AnnaController you are calling GetComponent, this returns null if there is no component of that type.

  • AnnaController.MvmntVctr, since I can’t see the AnnaController script, it’s possible that this is null.

@Rybo5000 @MewEight Thank you very much for all of this, you have in fact answered my initial question, however seeing as every leak patched creates another problem i have decided to approach this task in an entirely different manner. Dont worry, your efforts were not in vain, now i understand what this error means. Many Thanks :slight_smile: