Help with error CS0165: Use of unassigned local variable 'avoidDirection'

I do not understand why only this bit of code in bold is not working?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Sensor : MonoBehaviour
{
    const float sensorLength = 5;
    const float frontSensorStartingPoint = 1;
    const float frontSideSensorStartingPoint = 0.5f;
    const float frontSensorAngle = 30;
    public void Check(){
        Vector3 sensorPosition;
        RaycastHit hit;
        bool flag;
        float avoidDirection;

        sensorPosition = transform.position + (transform.forward * frontSensorStartingPoint);
        if(Physics.Raycast(sensorPosition, transform.forward, out hit, sensorLength)){
            Debug.Log(hit.normal.x);
            if(hit.normal.x < 0){
                avoidDirection = -1;
            }else{
                avoidDirection = 1;
            }
            Debug.DrawLine(transform.position, hit.point, Color.red);
        }

        //
        sensorPosition = transform.position + (transform.forward * frontSensorStartingPoint);
        sensorPosition += transform.right * frontSideSensorStartingPoint;
        Vector3 rightAngle = Quaternion.AngleAxis(frontSensorAngle, transform.up) * transform.forward;
        if(Physics.Raycast(sensorPosition, transform.forward, out hit, sensorLength)){
            [B][I]avoidDirection -= 1;[/I][/B]
            Debug.DrawLine(sensorPosition, hit.point, Color.black);
        }
        if(Physics.Raycast(sensorPosition, rightAngle, out hit, sensorLength)){
            avoidDirection -= 0.5f;
            Debug.DrawLine(sensorPosition, hit.point, Color.black);
        }

        //right sensors
    }
}

Read the error… it actually TELLS you precisely what the problem is.

If the if() statement on line 18 fails, avoidDirection will never be set. In other words, “unassigned.”

Either set it to zero at its declaration and be prepared for what that means when you use it later, or else do not execute code that uses the uninitialized variable.

Remember: NOBODY memorizes error codes. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

The complete error message contains everything you need to know to fix the error yourself.

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

1 Like