unity – NullReferenceException when object will not be null

unity – NullReferenceException when object will not be null

[ad_1]

So within the following code:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
utilizing UnityEngine.UI;
utilizing System;
utilizing System.Threading;

public class TreeHealthBar : MonoBehaviour
{
    public GameObject axe;

    public float healthBarSize;
    destroyTree destroy;
    RectTransform rectTransform;
    Vector3 dimension;

    Vector3 place;
    Picture img;

    void Begin()
    {
        destroy = axe.GetComponent<destroyTree>();
        rectTransform = GetComponent<RectTransform>();
        img = GetComponent<Picture>();
        img.enabled = false;
    }

    void Conceal()
    {
        img.enabled = false;
    }

    void Replace()
    {
        if (destroy??false)
        {
            if (destroy.touchingTree() && destroy.GetTreeHealth() > 0f)
            {
                img.enabled = true;

                CancelInvoke();

                dimension = remodel.localScale;

                dimension.x = destroy.GetTreeHealth() / 100f * healthBarSize;

                place = rectTransform.anchoredPosition;

                if (destroy.GetTreeHealth() < destroy.GetTreeMaxHealth())
                    place.x = 0.0f - 250.0f + (destroy.GetTreeHealth() * 5.0f) - ((50 - destroy.GetTreeHealth()) * 2.5f);
                else
                    place.x = 0.0f;

                remodel.localScale = dimension;

                rectTransform.anchoredPosition = place;
            }

            else
            {
                Invoke(nameof(Conceal), 1.5f);
            }

            if (destroy.touchingTree() && destroy.GetTreeHealth() <= 0)
            {
                img.enabled = false;
            }
        }
    }
}

it returns the NullReferenceException within the console: Object reference not set to an occasion of an object.
It’s on line 37, which is the place the if assertion is containing the “destroy” object. However the large downside is that it reveals this within the console when the axe (which the destroyTree is assigned to) is colliding with the tree. Sure, destroyTree is assigned, the whole lot has its elements and the whole lot is assigned. I even made it CHECK if it is null, together with attempting to make use of if (destroy != null) as a substitute of the ?? equation, nevertheless it nonetheless will not work. It is most likely answered someplace, however I truly do not even know what’s inflicting the issue. I additionally put && destroy??false within the line 37 if assertion, and that appeared to all the time present the bar, and never even change the dimensions or place. Ah, and the essential half is, I by no means acquired this error message after I simply used a dice, not a blender imported object. So it has one thing to do with that, and I did not even want if (destroy??false)

[ad_2]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply