object reference not set to instance of an object

I CANNOT understand what the problem is here. Won’t past the whole code, just the parts that matter and it seems to me like it should work. it’s a muzzle flash for a gun. one of many times I have used this exact same set up and they all work but for some reason I keep getting “Object reference not set to an instance of an object”. The first error being in the start function and then more for every time I actually settrigger or refer to the gameobject at all. Tried it with just the ani reference and this version also adding the gameObject itself, then making it public so I dragged it into the proper slot and unity still has no clue. The object is buried in the hierarchy of the actual sniper( ( like all enemy muzzleflashes that work) but even if I take it out and just lay it in the scene as a separate gameobject the code returns same errors. Driving me crazy!!! I see nothing different from this and the other guns that work - but “yes” all muzzleflashes have unique names so I can’t be confusing unity that way.


EDIT. fixed it just went back to the saved version with the working sniper and redid the grenade stuff and all is well. No clue what broke or why but the sniper scripts are EXACTLY the same. Fixed the situation in less than 20 minutes this way . wasted most of the day trying figure out why it broke rather than reverting. live and learn


 Animator ani;
public GameObject sniperMuzzleFlash;
void Start()
	{
		sniperMuzzleFlash = GameObject.Find ("sniperMuzzleflash");
		ani = GameObject.Find("sniperMuzzleflash").GetComponent <Animator> ();
}
void OnTriggerEnter(Collider other)
	{
		if(other.tag == "Player")
		ani.SetTrigger ("muzzleFlash");

First thing I would note is that you are doing a GameObject.Find to store a ref to the game object, but then do another find on that object instead of using the reference directly. Try running this code, it should at least tell you where the error is specifically. If you see the first log then one of two things must be true, either no object exists with that name, OR it does exist and it is disabled (Find only returns active game objects).

void Start()
    {
        sniperMuzzleFlash = GameObject.Find("sniperMuzzleflash");
        if (sniperMuzzleFlash == null)
        {
            Debug.Log("Failed to find game object: sniperMuzzleflash");
        }
        else
        {
            ani = sniperMuzzleFlash.GetComponent<Animator>();
            if (ani == null)
            {
                Debug.Log("Failed to find Animator component on object sniperMuzzleflash");
            }
        }
    }