0

I am building a RPG game in Unity3d, and I came across this error "NullReferenceException: Object reference not set to an instance of an object" and I really don't get what is wrong.

My code is this:

using UnityEngine;
using RPG.Move;

namespace RPG.Lupta
{
    public class Luptator : MonoBehaviour
    {
        [SerializeField] float distantaArma =2f;
        
        Transform tinta;
        
        private void Update()
        {
            bool isInRange = GetIsInRange();

            if (tinta != null & !GetIsInRange())
            {
                GetComponent<Mover>().MutareCatre(tinta.position);
            }
            else
            {
                GetComponent<Mover>().Stop();
            }
        }

        private bool GetIsInRange()
        {
            return Vector3.Distance(transform.position, tinta.position) < distantaArma;
        }


        public void Atac(TintaLupta tintaLupta)
        {
            tinta = tintaLupta.transform;
        }

        public void Cancel ()
        {
            tinta = null;
        }

    }

}

It says that the error is on line 14 and 28, which are these :

Line 28: return Vector3.Distance(transform.position, tinta.position) < distantaArma;

Line 14: bool isInRange = GetIsInRange();

Does anyone know what the problem might be?

Igor
  • 58,317
  • 10
  • 91
  • 160
Lavinia Ioana
  • 37
  • 1
  • 5
  • The variable `transform` in the statement `transform.position` might be `null`. Hence the null refrence error. Try to debug your code and set a watch on the variable mentionned previously. – Rivo R. May 26 '22 at 16:02
  • tinta is null when get inTheRange running: private void Update() { if (!tinta) GetComponent().Stop(); else if (!GetIsInRange()) GetComponent().MutareCatre(tinta.position); } – KiynL May 26 '22 at 16:03
  • I tried this but it doesn t help, nothing seems to hell, idk why – Lavinia Ioana May 26 '22 at 18:40

0 Answers0