I’ve created a script which I attach to an empty game object, the idea of the script is to cast 2 rays which will hit the ground in my game, I will then use the location of the 2 hit’s and the origin of the game object to draw a triangular mesh. The mesh is to represent a vision cone from an enemy which travels along the top of the screen.
Here is the error
(it’s occuring on line 42: “vertexTwo.x = hitPointLeft.transform.position.x;”)
ERROR:
“NullReferenceException: Object reference not set to an instance of an object
rayCast.generateMesh () (at Assets/Scripts/Cloud Monster/rayCast.cs:42)
rayCast.Update () (at Assets/Scripts/Cloud Monster/rayCast.cs:34)”
Here is the script:
using UnityEngine;
using System.Collections;
public class rayCast : MonoBehaviour {
//Set up public ints for angle to rayCast at
public int leftAngle = 0;
public int rightAngle = 0;
//Create two vector2's to represent the directions of the two rayCasts;
Vector2 directionLeft = new Vector2 ();
Vector2 directionRight = new Vector2 ();
//Create two rayCast2D's to represent the collision locations of the rayCasts which will be updated each frame
RaycastHit2D hitPointLeft = new RaycastHit2D ();
RaycastHit2D hitPointRight = new RaycastHit2D ();
//Set the directions up
void Start () {
//Always keep the y values at -100 (i.e. well below the stage),
//then we can just update the x values to change where the cone is "looking"
directionLeft.x = this.transform.rotation.x - leftAngle;
directionLeft.y = this.transform.rotation.y - 100;
directionRight.x = this.transform.rotation.x + rightAngle;
directionRight.y = this.transform.rotation.y - 100;
}
//Update the left/right hitPoint variables with the new collision location
//After I've got this set up I'll add in updating the rayCast direction each frame as well.
void Update () {
//This puts the if the rayCast is hitting something, then puts the coordinates of the collision into the rayCast2D objects
hitPointLeft = Physics2D.Raycast (this.transform.position, directionLeft);
hitPointRight = Physics2D.Raycast (this.transform.position, directionRight);
//Run the generateMesh function to update the mesh position
generateMesh ();
}
void generateMesh () {
Debug.Log (hitPointLeft.transform);
//Create a vector3 to represent the 2nd vertex of the triangle
Vector3 vertexTwo = new Vector3();
vertexTwo.x = hitPointLeft.transform.position.x;
vertexTwo.y = hitPointLeft.transform.position.y;
vertexTwo.z = 0; //z = 0 because we're 2D
//Create a vector3 to represent the 3rd vertex of the triangle
Vector3 vertexThree = new Vector3();
vertexThree.x = hitPointRight.transform.position.x;
vertexThree.y = hitPointRight.transform.position.y;
vertexThree.z = 0; //z = 0 because we're 2D
//Create a vector3 array to store the vertex locations
Vector3[] meshVerts = new Vector3[3];
meshVerts[0] = this.transform.position;
meshVerts[1] = vertexTwo;
meshVerts[2] = vertexThree;
//Apply the vertex array to the mesh
Mesh visionCone = new Mesh();
visionCone.vertices = meshVerts;
visionCone.RecalculateBounds();
//Add the mesh to a meshFilter attached to the game object
this.gameObject.GetComponent<MeshFilter> ().mesh = visionCone;
Graphics.DrawMeshNow(this.gameObject.GetComponent<MeshFilter>().mesh, this.transform.position, this.transform.rotation);
}
//This will be run each frame and will remove the previous frames mesh
//STILL NEEDS DOING!
void garbageCollection () {
}
}