- Home /
Object Reference not set to instance of object
Here's the code
using UnityEngine;
using System.Collections;
public class NPCComponent : MonoBehaviour {
public NPCHealthHandler nph;
public NPCStats nps;
public NPCCollisionHandler npch;
// Use this for initialization
void Start () {
nph.GetComponent<NPCHealthHandler>();
nps.GetComponent<NPCStats>();
npch.GetComponent<NPCCollisionHandler>();
}
// Update is called once per frame
void Update () {
}
}
errors pulling on the line npch.GetComponent();
Answer by LPGaming · Jan 15, 2013 at 12:05 AM
Fixed it, was using GetComponent instead of AddComponent, because I was feeling stupid.
Answer by Lovrenc · Jan 14, 2013 at 10:59 PM
It seems like you didnt set npch
in your inspector.
Other than that, why are you serching for those components if you never save the reference?
I'm using handlers for everything, they are all called in other scripts. That way I don't have to have a separate script for each NPC, Object, etc. I have a handler that sets all of a NPC's stats, one that handles all their health, and one that handles all the NPC based collision, and for the player side (Which has no errors) I have AttackHandler,StatHandler,CollisionHandler,$$anonymous$$ovementHandler,InterfaceHandler
Also, I don't use the inspector, the point of this script is to make it so I don't have to.
I was being an idiot and using GetComponent ins$$anonymous$$d of Add, my bad, Thanks thought
Your answer
![](https://koobas.hobune.stream/wayback/20220613090943im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Object reference not set to an instance of an Object 1 Answer
Distribute terrain in zones 3 Answers
Disabling A Script on a GameObject From a Different Script 2 Answers