Hello, I’m very new to game development. I am making an RPG and I want damage numbers to pop up when hitting an enemy. And I get an error:
NullReferenceException: Object reference not set to an instance of an object
HurtEnemy.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/HurtEnemy.cs:33)
Apologies if this question has been asked before, but I can’t seem to find an answer. Thanks in advance 
Here’s my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HurtEnemy : MonoBehaviour {
public int damageToGive;
public GameObject damageBurst;
public Transform hitPoint;
public GameObject damageNumber;
private int GameObject;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == “Enemy”)
{
other.gameObject.GetComponent().HurtEnemy(damageToGive);
Instantiate(damageBurst, hitPoint.position, hitPoint.rotation);
var clone = Instantiate(damageBurst, hitPoint.position, Quaternion.Euler(Vector3.zero));
clone.GetComponent().damageNumber = damageToGive; <Visual studio points to this line.
clone.transform.position = new Vector2(hitPoint.position.x, hitPoint.position.y);
}
}
}
First, please look at this thread for how to post code nicely on the forums: Using code tags properly - Unity Engine - Unity Discussions
Next, something on that line is null. Check like this:
FloatingNumbers fn = clone.GetComponent<FloatingNumbers>();
if(fn == null) {
print("no floating numbers component was found on the clone.");
}
Also note that you are instantiating 2 ‘damageBurst’, one with no retained reference, and the other as ‘clone’. I’m just mentioning this in case that wasn’t intentional.
Sorry about the code posting, I didn’t know
I put in the code and the error is not there anymore, but the numbers do not appear and the print comes up.
Right, well the code wasn’t complete… it was just the portion to confirm that is what’s null.
The object you’re instantiating doesn’t have that component (script) attached. So, check the prefab/game object and put one on there 
Then, try your original code, again.
I attached the FloatingNumbers script to DamageBurst which has fixed the error, but yet again, the numbers aren’t showing.
Are they UI Text objects? If so, you would need to add them to a canvas.
If something else, please explain.
Do you not even see them in the hierarchy at all - maybe in the wrong place?
I’ll try and explain as best I can. The numbers are in a canvas. They aren’t supposed to be the hierarchy. The idea is that one would appear whenever you hit an enemy.
Here’s the FloatingNumbers script if that helps in any way.
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine;
public class FloatingNumbers : MonoBehaviour {
public float moveSpeed;
public int damageNumber;
public Text displayNumber;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
displayNumber.text = "" + damageNumber;
transform.position = new Vector3(transform.position.x, transform.position.y + (moveSpeed * Time.deltaTime), transform.position.z);
}
}
Right, okay, but if you didn’t set its parent when you instantiated it, it won’t be in the canvas (or does the prefab come with a canvas?)
Sorry, little confused. 
Can you see the spawned prefab after your code runs. Is it in the right spot? Is the text visible or the rect too small or… ?
The text came as a child of a canvas when I created it. Whenever I hit an enemy the prefab doesn’t seem to appear in the hierarchy. I get a clone of the damage burst (which is a particle effect of blood).
Okay, so that was part of the confusion. You try to get the component on there to set the text to display, but that’s not the right game object.
So earlier when you added the script to it, to get rid of the error, we were doing the wrong thing.
You want to be instantiating Text objects (or using pooled ones) or something like that.
Right?
I’m not sure to be honest, I followed a tutorial which said to do so. As far as I understand it is supposed to appear at the weapon. Sorry I’m not being very helpful with clarifying the confuson here, this is the first ever game I created and I have very little clue as to what is going on 
Ya, I understand… and that’s okay and understandable. I mean, I had the wrong idea that I thought your text was supposed to be connected to what you were adding (your effect).
Anyways, I would suggest that if you’d like to work with unity, you should begin with very easy tutorials; using unity, scripting, and small, simple games.
This will let you build up your knowledge.
If you’re enjoying the tutorial you’re currently working on, you could try to go back & look over what’s said and see if you missed a step… 
Thanks for the help. The tutorial is very in-depth and easy to understand. Everything was fine until this very moment. I’ll try and redo everything in the tutorial and hope that that fixes it if not, I’ll just go without it. Thanks again 
Alright… well, there’s nothing wrong with looking back over the tutorial, especially if you’re liking it.
If you happen to not find the fix for the text, just finish up the rest of it.
You can go back and add it later. Adding text to appear, as you want, won’t be difficult to figure out in a bit of time for you.
My best guess, is that you just need a text object to instantiate, set its parent as the canvas (or a certain canvas, if more than 1), and the script you already have…
*though, that script could be improved by just setting the text once, and not always in update. 
– for instance, add a public method that takes the damage amount as a parameter, pass that in from the other script, instead of modifying the field directly, and at that point, set the text. That’s it.
Just make sure if disables itself after a certain time.
Good luck 
I figured it out. You named the problem right from the start. damageBurst was instantiated twice and that should not be like that. One instantiate should be damageBurst and the clone one was damageNumber. I guess there was no reason for the long chain of confusion after all
Thanks again 
Hey, that’s great… I’m glad you pin pointed it & got it resolved. 
Even if it takes some time (or a day…) – we’ve all been there.
Take care. =)