0
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Explosion : MonoBehaviour
{
    Animator anim;
    // Start is called before the first frame update
    void awake()
    {
        anim = GetComponent <Animator>(); 
    }
    private void OnEnable()
    {
        Invoke("OnDisable", 2f);        
    }
    private void OnDisable()
    {
        gameObject.SetActive(false);
    }

    // Update is called once per frame
    public void StartExplosion(string target)
    {
        Debug.Log("startExplosion");
        anim.SetTrigger("OnExplosion"); 

        Debug.Log("startExplosion2");
        switch (target)
        {
            case "doge":
                transform.localScale = Vector3.one * 1.2f;
                break;
            case "dogelonmas":
                transform.localScale = Vector3.one;
                break;
            case "RocketA":
                transform.localScale = Vector3.one;
                break;
            case "RocketB":
                transform.localScale = Vector3.one;
                break;
            case "samo":
                transform.localScale = Vector3.one;
                break;
            case "shiba":
                transform.localScale = Vector3.one;
                break;
            case "jindoge":
                transform.localScale = Vector3.one;
                break;
        }

        Debug.Log("startExplosion3");
    }
}

enter image description here

Hey guys, I am dying from frustration now, I'd be super grateful if anybody can help.

So I am getting the error "NullReferenceException : Object reference not set to an instance of an object" when it tries to execute anim.SetTrigger in my StartExplosion().

It is extremly frustrating to keep getting this error. As you can see I put a trigger "OnExplosion" for the condition of the explosion, and there is never a type and set up everything correctly I believe.

W 3
  • 19
  • 2
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Max Play Apr 29 '22 at 15:35

1 Answers1

0

I see that your Animator anim is in a method called: awake(). It is important that you check the capitalization of your methods. I'm guessing you wanted to use the Awake() method that Unity has built-in. However, awake() and Awake() are not the same. Since awake() does not get called when the script instance is loaded, the method awake() never gets used and Animator anim never gets set to anything. To resolve this:

Animator anim;
private void Awake()
{
  anim = gameObject.GetComponent<Animator>();
}