[SOLVED]getting error when I reference a bool (changing in update) from another script

so why do I get “Object reference not set to an instance of an object” error in console. Both scripts are on the same gameObject btw. I’d appreciate it if someone helped me figure out the problem.

Because a variable you’re calling is null. You have to provide more info if anyone should be able to help.

ok, the bool i’m trying to check if true or false is :

grounded = Physics2D.Linecast (transform.position, groundPoint.position,groundLayer);

my code for check from the other script :

void FixedUpdate () {
if (running && verticalMovements.grounded ) {

body.velocity = new Vector2 (speed, body.velocity.y);
}


sorry i didn’t get the whole script cuz it’s too long

Yes, but which exact line is it that gives you the error? Double-clicking on the error shows you which line it happens on. I don’t even know if any of these lines is the one giving you the error. Maybe verticalMovements is null? check with Debug.Log(verticalMovements); and see if it’s null. Also, use code-tags when posting scripts, it’s easier to read etc.

1 Like

that error basically means that you are trying to get a variable that wasn’t set to anything. consider the following

bool isGrounded;

gameObject target;

is isGrounded true? or false? what is target set to? the compiler doesn’t know cause you didn’t give it a default (it won’t even assume false and null in some cases). if i recall correctly, due to the inspector auto-initalizing public struct types in monobehaviours, they’ll assume a default for you, but private structs and reference types may not initalize correctly. so its best that you explicitly define one

bool isGrounded = false;

gameObject target = null;

OH MAN! it was really null! i forgot to assign verticalMovements to the class on my gameobject. Thanks!!