-1

(Unity3d) I'm getting this error related to the "if(raytest.obstacleExists==true)" Here's the main script:

private Raytest raytest;

void Start()
{
    target = new Vector3(5f, 0f, 0f);
    obTarget = new Vector3(obstacle.transform.position.x - 1.3f, 0f, 0f);
    raytest = GetComponent<Raytest>();
}

public void Movement()
{
    float step = speed * Time.deltaTime;

    //if there is an obstacle ahead
    //stop 1-2 meters before him
    //else
    //go to target position

    if(runCode==true)
    {
        if(raytest.obstacleExists==true)
        {
            transform.position = Vector3.MoveTowards(transform.position, obTarget, step);
        }

        else
        {
            transform.position = Vector3.MoveTowards(transform.position, target, step);
        }
    }
}

Does anyone knows why is this happening?

ferd
  • 1
  • There is no such thing as a boolean null reference. – TheGeneral Jan 18 '21 at 23:26
  • As @00110001 mentioned boolean is a value type, so you cannot get boolean null reference exception, the problem might be you **private Raytest raytest;** is not instantiated or referred to any object. You might want to provide more information about the error as it hard to figure out what's the exact problem with our code – Miraziz Jan 18 '21 at 23:29
  • 1
    `GetComponent` "*Returns the component of Type type if the game object has one attached, null if it doesn't.*" – TheGeneral Jan 18 '21 at 23:30

1 Answers1

1

raytest seems to be not instantiated. Check if your variable is null or not.

if(raytest?.obstacleExists==true)
{
    transform.position = Vector3.MoveTowards(transform.position, obTarget, step);
}
Sajjad Mortazavi
  • 190
  • 2
  • 12