Object reference not set to an instance of an object with raycast2d
Hello! So i see many people posting about the same error but not with my exact needs. So I am currently learning Unity and thus im playing around with stuff. My Plan is to make a one way platform that lets you fall through after you stand on it and press a serten key. The good thing is, it works but globally. I then proceeded to look into raycasts and tried to use them to detect, wether im on such a platform or not. Another good thing it also works! but now is the bad thing. Everytime I am in midair and press S i get the NullReferenceException: Object reference not set to an instance of an object.
now to my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VerticalPlatform : MonoBehaviour
{
public static PlatformEffector2D effector;
public static float waitTime;
private bool platformRequest = false;
// Start is called before the first frame update
void Start()
{
effector = GetComponent<PlatformEffector2D>();
}
// Update is called once per frame
void Update()
{
//THIS IS THE LINE 22 ERROR
Fall();
}
public static void Fall()
{
if (Input.GetKeyUp(KeyCode.S))
{
waitTime = 0.5f;
}
//THIS IS THE LINE 31 ERROR
if (Input.GetKeyDown(KeyCode.S) && PlayerController.checkForPlatform.transform.tag == "Platform")
{
if (waitTime <= 0)
{
effector.rotationalOffset = 180f;
waitTime = 0.5f;
}
else
{
waitTime -= Time.deltaTime;
}
}
if (Input.GetKey(KeyCode.Space))
{
effector.rotationalOffset = 0;
}
}
}
The thing to note is, that i use the raycast on my Playercontroller script wich sits in the update function like this: checkForPlatform = Physics2D.Raycast(groundCheck.position, Vector2.down, 2f);
I declared the variable up top as public static RaycastHit2D checkForPlatform;
Now why does it spam this error even tho it works just fine?
Your answer
Follow this Question
Related Questions
CS0117 Error 0 Answers
[solved] Collision damage and Health Controller dont work 2 Answers
unexpected void error 1 Answer
IndexOutOfRangeException in TextureInspector 1 Answer
RayCast 2d change start position ? 0 Answers