[ad_1]
So in the following code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Threading;
public class TreeHealthBar : MonoBehaviour
{
public GameObject axe;
public float healthBarSize;
destroyTree destroy;
RectTransform rectTransform;
Vector3 size;
Vector3 position;
Image img;
void Start()
{
destroy = axe.GetComponent<destroyTree>();
rectTransform = GetComponent<RectTransform>();
img = GetComponent<Image>();
img.enabled = false;
}
void Hide()
{
img.enabled = false;
}
void Update()
{
if (destroy??false)
{
if (destroy.touchingTree() && destroy.GetTreeHealth() > 0f)
{
img.enabled = true;
CancelInvoke();
size = transform.localScale;
size.x = destroy.GetTreeHealth() / 100f * healthBarSize;
position = rectTransform.anchoredPosition;
if (destroy.GetTreeHealth() < destroy.GetTreeMaxHealth())
position.x = 0.0f - 250.0f + (destroy.GetTreeHealth() * 5.0f) - ((50 - destroy.GetTreeHealth()) * 2.5f);
else
position.x = 0.0f;
transform.localScale = size;
rectTransform.anchoredPosition = position;
}
else
{
Invoke(nameof(Hide), 1.5f);
}
if (destroy.touchingTree() && destroy.GetTreeHealth() <= 0)
{
img.enabled = false;
}
}
}
}
it returns the NullReferenceException in the console: Object reference not set to an instance of an object.
It is on line 37, which is where the if statement is containing the “destroy” object. But the big problem is that it shows this in the console when the axe (which the destroyTree is assigned to) is colliding with the tree. Yes, destroyTree is assigned, everything has its components and everything is assigned. I even made it CHECK if it’s null, including trying to use if (destroy != null)
instead of the ?? equation, but it still won’t work. It’s probably answered somewhere, but I actually don’t even know what’s causing the problem. I also put && destroy??false
in the line 37 if statement, and that seemed to always show the bar, and not even change the size or position. Ah, and the important part is, I never got this error message when I just used a cube, not a blender imported object. So it has something to do with that, and I didn’t even need if (destroy??false)
[ad_2]