This question was
closed Jul 04, 2020 at 01:15 AM by
Sevotastic for the following reason:
Was answered
Question by
Sevotastic · May 18, 2020 at 06:33 AM ·
errorbugpysics
Physics2D.BoxCast ERROR
Im lost because i have no idea why unity is creating an error in the boxcasting section, this is the code:
RaycastHit2D lefty = Physics2D.BoxCast(origin, size, 0f, new Vector2(-1, 0), distance, layerNum); /*layerNum brings up errors:
NullReferenceException: Object reference not set to an instance of an object
WallData.CastRay()(at Assets / TestScrpits / Draggables / WallData.cs:44)
WallData.Update()(at Assets / TestScrpits / Draggables / WallData.cs:34) */
if (lefty.collider.gameObject.GetComponent<WallData>()) // <-- This brings up an error (All wall gameObjects have WallData.cs and are layer 14).
{
left = true;
print("YES");
}
else
{
left = false;
Debug.LogError("NO");
}
//Origin = transform.position.x & transform.position.y
//Size = this gameObjects box component size x & y
//0f is the angle
//the new vector2 is the direction
//Distance = 1f
//LayerNum = 14
I just dont understand why a null reference exception is popping up. The erros in the code that unity is pointing out: The first error in CastRay();
if (lefty.collider.gameObject.GetComponent<WallData>())
The second error in Update();
private void Update()
{
CastRay();
}
Comment