using UnityEngine;
using System.Collections;
[RequireComponent (typeof (BoxCollider2D))]
public class Controller2D : MonoBehaviour {
const float skinWidth = .015f;
public int horizontalRayCount = 4;
public int verticalRayCount = 4;
float horizontalRaySpacing;
float verticalRaySpacing;
BoxCollider2D collider;
RaycastOrigins raycastOrigins;
void start()
{
collider = GetComponent<BoxCollider2D>();
}
void Update()
{
UpdateRaycastOrigins ();
calculateRayspacing ();
for (int i = 0; i < verticalRayCount; i++) {
Debug.DrawRay (raycastOrigins.bottomLeft + Vector2.right * verticalRaySpacing * i, Vector2.up * -2, Color.red);
}
}
void UpdateRaycastOrigins()
{
Bounds bounds = collider.bounds; // **error Line 38**
bounds.Expand(skinWidth * -2);
raycastOrigins.bottomLeft = new Vector2 (bounds.min.x, bounds.min.y);
raycastOrigins.bottomRight = new Vector2 (bounds.max.x, bounds.min.y);
raycastOrigins.topLeft = new Vector2 (bounds.min.x, bounds.max.y);
raycastOrigins.topRight = new Vector2 (bounds.max.x, bounds.max.y);
}
void calculateRayspacing()
{
Bounds bounds = collider.bounds;
bounds.Expand (skinWidth * -2);
horizontalRayCount = Mathf.Clamp (horizontalRayCount, 2, int.MaxValue);
verticalRayCount = Mathf.Clamp (verticalRayCount, 2, int.MaxValue);
horizontalRaySpacing = bounds.size.y / (horizontalRayCount - 1);
verticalRaySpacing = bounds.size.x / (verticalRayCount - 1);
}
struct RaycastOrigins
{
public Vector2 topLeft, topRight;
public Vector2 bottomLeft, bottomRight;
}
}
…
error is:
- NullReferenceException: Object reference not set to an instance of an object
Controller2D.UpdateRaycastOrigins () (at Assets/Scripts_/Controller2D.cs:38)
Controller2D.Update () (at Assets/Scripts_/Controller2D.cs:26)
And some warnings are:
-
Assets/Scripts_/Controller2D.cs(14,23): warning CS0108:
Controller2D.collider' hides inherited member
UnityEngine.Component.collider’. Use the new keyword if hiding was intended -
Assets/Scripts_/Controller2D.cs(11,15): warning CS0414: The private field `Controller2D.horizontalRaySpacing’ is assigned but its value is never used
Please suggest the solution to solve this code.