Question by
huseyinbaba58 · Jul 30, 2020 at 11:12 AM ·
unity 5
How to fix Missing Reference Exception?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DusmanSaldirisi : MonoBehaviour
{
public Transform hedef; //Hedef nesnesi ayarlandı.
public GameObject nesne;//Bu hangi nesne alınacak onu gösterir.
public Slider can; //Burada can göstergesi yapılır.
public float hasar = 20f; //Hasar 20f olacak.
public float enYuksekSaglik = 100f;
public float surat = 5.0f; //5f lik bir sürat yapılacak.
string canDurumu; //Bu can durumunu yazılı ifade eder.
void Start()
{
canDurumu = enYuksekSaglik.ToString();//Can Durumunu bildirir.
can = GetComponent<Slider>(); //Kaydırıcı bileşeninin özelliklerine ulaşmaktayız.
}
void Update()
{
enYuksekSaglik = Mathf.Clamp(enYuksekSaglik, 0f, 100f);//Sağlık değeri 0 ile 100 arasında olacak.
enYuksekSaglik -= hasar;
if (enYuksekSaglik == 0)
{
Debug.Log("Nesne yok edildi");
}
Vector3 yonelme = new Vector3(hedef.position.x, hedef.position.y, hedef.position.z); //Bu hedefin bulunduğu noktaları temel alır.
transform.position = Vector3.MoveTowards(transform.position, yonelme, surat * Time.deltaTime);
}
}
Error Message is
MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Transform.get_position () <0x3b566220 + 0x0006a> in <75a427cddb804b08b58375a65a802d17>:0
DusmanSaldirisi.Update () (at Assets/DusmanSaldirisi.cs:32)
Comment