S
S
Sergey Orobinsky2019-03-06 15:31:46
Unity
Sergey Orobinsky, 2019-03-06 15:31:46

Error in Unity NullReferenceException: Object reference not set to an instance of an object and what to do with it?

There is 1st script

using UnityEngine;
using System.Collections;

public class CollectableBounce : MonoBehaviour 
{
  
    private SpriteRenderer render;
    private int damage = 1; 
    private GameObject particle;
    private GameObject heart;

  void Start ()
  {
        particle = GameObject.Find("Healling");
        render = GetComponent<SpriteRenderer>();
        heart = GameObject.Find("Healths");
  }

  // Update is called once per frame
  void Update ()
  {
  
  }

  void OnTriggerEnter2D(Collider2D collider)
  {
    if (collider.gameObject.name == "PlayerCharacter")
    {
            int lasthp = heart.GetComponent<UIControler>().health;
            int futurehp = heart.GetComponent<UIControler>().health + damage;
            heart.GetComponent<UIControler>().health = futurehp;
            heart.GetComponent<UIControler>().GetHit(damage, lasthp, futurehp);
            render.enabled = false;
            particle.transform.position = new Vector2(this.transform.position.x, this.transform.position.y);
            particle.GetComponent<ParticleSystem>().Play();
      Destroy(gameObject);
    }
  }
}

And 2nd script
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UIControler : MonoBehaviour {

    public int health = 10;
    public GameObject player;
    public Animator animator;
    public SpriteRenderer render;
  // Use this for initialization
  void Start () {
        render = GetComponent<SpriteRenderer>();
        animator = GetComponent<Animator>();
        
  }

    void Update()
    {

    }
    public void GetHit(int damage, int lasthp, int futurehp)
    {
        if(lasthp == 10 && damage == 2 && futurehp == 8)
        {
            animator.SetTrigger("10HPdown");
        }
        if (lasthp == 9 && damage == 2 && futurehp == 7)
        {
            animator.SetTrigger("9HPdown");
        }
        if (lasthp == 8 && damage == 2 && futurehp == 6)
        {
            animator.SetTrigger("8HPdown");
        }
        if (lasthp == 7 && damage == 2 && futurehp == 5)
        {
            animator.SetTrigger("7HPdown");
        }
        if (lasthp == 6 && damage == 2 && futurehp == 4)
        {
            animator.SetTrigger("6HPdown");
        }
        if (lasthp == 5 && damage == 2 && futurehp == 3)
        {
            animator.SetTrigger("5HPdown");
        }
        if (lasthp == 4 && damage == 2 && futurehp == 2)
        {
            animator.SetTrigger("4HPdown");
        }
        if (lasthp == 3 && damage == 2 && futurehp == 1)
        {
            animator.SetTrigger("3HPdown");
        }
        if (lasthp == 2 && damage == 2 && futurehp == 0)
        {
            animator.SetTrigger("2HPdown");
            PlayerMovement1 se = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMovement1>() as PlayerMovement1;
            se.isDead = true;
        }
        if (lasthp == 1 && damage == 2 && futurehp == -1)
        {
            animator.SetTrigger("1HPdown");
            PlayerMovement1 se = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMovement1>() as PlayerMovement1;
            se.isDead = true;
        }
        if (lasthp == 9 && damage == 1 && futurehp == 10)
        {
            animator.SetTrigger("9HPup");
        }
        if (lasthp == 8 && damage == 1 && futurehp == 9)
        {
            animator.SetTrigger("8HPup");
        }
        if (lasthp == 7 && damage == 1 && futurehp == 8)
        {
            animator.SetTrigger("7HPup");
        }
        if (lasthp == 6 && damage == 1 && futurehp == 7)
        {
            animator.SetTrigger("6HPup");
        }
        if (lasthp == 5 && damage == 1 && futurehp == 6)
        {
            animator.SetTrigger("5HPup");
        }
        if (lasthp == 4 && damage == 1 && futurehp == 5)
        {
            animator.SetTrigger("4HPup");
        }
        if (lasthp == 3 && damage == 1 && futurehp == 4)
        {
            animator.SetTrigger("3HPup");
        }
        if (lasthp == 2 && damage == 1 && futurehp == 3)
        {
            animator.SetTrigger("2HPup");
        }
        if (lasthp == 1 && damage == 1 && futurehp == 2)
        {
            animator.SetTrigger("1HPup");
        }
    }
}

and hence the error
NullReferenceException: Object reference not set to an instance of an object
CollectableBounce.OnTriggerEnter2D (UnityEngine.Collider2D collider) (at Assets/HyperLuminal/2D Fantasy Forest Tileset/Scripts/CollectableBounce.cs:29)

What to do with her?
Possibly a hint

ещё есть скрипт, который идентичен 1-му:
using UnityEngine;
using System.Collections;

public class CollectableDamage : MonoBehaviour
{
    private SpriteRenderer render;
    private int damage = 2;
    private GameObject particle;
    private GameObject heart;
    void Start()
    {
        particle = GameObject.Find("Boom");
        render = GetComponent<SpriteRenderer>();
        heart = GameObject.Find("Healths");
    }

    // Update is called once per frame
    void Update()
    {
      
    }
    void OnTriggerEnter2D(Collider2D collider)
    {
        if (collider.gameObject.name == "PlayerCharacter")
        {
            int lasthp = heart.GetComponent<UIControler>().health;
            int futurehp = heart.GetComponent<UIControler>().health - damage;
            heart.GetComponent<UIControler>().health = futurehp;
            heart.GetComponent<UIControler>().GetHit(damage, lasthp, futurehp);
            render.enabled = false;
            particle.transform.position = new Vector2(this.transform.position.x, this.transform.position.y);
            particle.GetComponent<ParticleSystem>().Play();
            Destroy(gameObject);
        }
    }
}

Но на него не ругается...

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question