The question is answered, right answer was accepted
object reference not set to an instance of an object
THANKS IN ADVANCE :)
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.:D The Debug.Log was just to check if i had made an error with my GetKeyDown
Answer by Rybo5000 · Mar 28, 2016 at 08:07 AM
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.
Yes i have already drag dropped all the public values i assigned in the initialization, and i believe that the AnnaController type does exist This is the AnnaController script itself: public class AnnaController : $$anonymous$$onoBehaviour {
Rigidbody2D rgdbdy;
Animator anmtr;
public Vector2 $$anonymous$$vmntVctr;
private static bool playerExists;
// Use this for initialization
void Start () {
rgdbdy = GetComponent<Rigidbody2D> ();
anmtr = GetComponent<Animator> ();
if (!playerExists) {
playerExists = true;
DontDestroyOnLoad (transform.gameObject);
} else {
Destroy (gameObject);
}
}
// Update is called once per frame
void Update () {
Vector2 $$anonymous$$vmntVctr = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
if ($$anonymous$$vmntVctr != Vector2.zero) {
anmtr.SetBool ("isWalking", true);
anmtr.SetFloat ("inputX", $$anonymous$$vmntVctr.x);
anmtr.SetFloat ("inputY", $$anonymous$$vmntVctr.y);
} else {
anmtr.SetBool ("isWalking", false);
}
rgdbdy.$$anonymous$$ovePosition (rgdbdy.position + $$anonymous$$vmntVctr * Time.deltaTime);
}
}
Have you set playerExists to true in the inspector mistakenly as that would destroy the gameobject.
Also another problem I've noticed, in your Update() function you've re-declared $$anonymous$$vmntVctr, so the one at the top will never be set to anything.
O$$anonymous$$! I've just noticed something. while the game is in the idle mode all the public variables are assigned, there are no problems. However as soon as i hit he play button the AnnaController variable is instantly removed and so in order for the instantiation to work i have to drag the Gameobject into the slot after hitting play. is it possible for me to be able to keep the component in the slot while i hit play
Answer by mewtwodan8 · Mar 29, 2016 at 01:40 PM
@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 :)
Follow this Question
Related Questions
Instantiate object in 2D game? 3 Answers
How to instantiate a static 2D object facing the player 0 Answers
Instantiating object randomly withing the boundaries of an object. 0 Answers
Creating Random object in random axis 0 Answers
Shot 2D Projectile in mouse direction always with same speed 1 Answer