I
I
Ilya Saveliev2020-08-29 13:00:38
C++ / C#
Ilya Saveliev, 2020-08-29 13:00:38

Object reference not set to an instance of an object. How is this possible?

Checking if an object is not null. And the call line gives an error:
Object reference not set to an instance of an object.
How is this even possible?

if(HitMirrorBP != null)
 {
     HitMirrorBP.Mirror();
 }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Korotenko, 2020-08-29
@firedragon

What is mirror equal to?

I
Ilya Savelyev, 2020-08-29
@MR_IKS

maskHit = Physics2D.Raycast(transform.position, Vector2.down, 10f, NeedLayer);
        if (maskHit.transform != null)
        {
            if(maskHit.collider.tag == "Mirror")
            {
                Debug.DrawLine(transform.position, maskHit.point, Color.red);
                HitMirrorBP = maskHit.transform.GetComponent<Mirror_BP>();
                if(HitMirrorBP != null)
                {
                    HitMirrorBP.Mirror();
                }

The question is, how can there be this error if the condition is true?
Mirror() is a function in another object's script.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question