I am new to Unity so I am not sure what is going on. I am trying to draw lines with some mouse clicks. However, I keep getting a NullReferenceException
on my camera. I’m attempting to utilize a command pattern in my environment. If my line command is NOT being called through the invoker. It runs fine. If its being called through the invoker, i get the null reference on the camera. I am sure I am making a simple mistake somewhere
Here’s my invoker class CommandInvoker.cs
using System.Collections.Generic;
using UnityEngine;
public class CommandInvoker : MonoBehaviour
{
static Queue<ICommand> CommandBuffer;
private void Awake()
{
CommandBuffer = new Queue<ICommand>();
}
public static void AddCommand(ICommand command)
{
CommandBuffer.Enqueue(command);
}
// Update is called once per frame
void Update()
{
if(CommandBuffer.Count > 0)
{
ICommand c = CommandBuffer.Dequeue();
if(c != null)
{
c.Execute();
}
}
}
}
Here is my DrawLineCommand.cs
using UnityEngine;
public class DrawLineCommand : ICommand
{
public Line MyLine;
public DrawLineCommand()
{
Debug.Log("Draw Line Command firing");
}
public void Execute()
{
Debug.Log("We are firing a line command");
MyLine.InitLine();
}
}
and here is my Line.cs
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class Line : MonoBehaviour
{
private LineRenderer _lRenderer;
private List<Vector3> _Vertices = new List<Vector3>();
public Material material;
private void Start()
{
this.InitLine();
}
private void Update()
{
try
{
if (Input.GetMouseButtonDown(0))
{
Ray CamRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(CamRay, out hit))
{
if (hit.transform.GetComponent<Renderer>().name == "MyGround")
{
EditorUtility.DisplayDialog("Some Title", "This is my message", "are we ok?");
}
}
}
}
catch (System.Exception Ex)
{
throw new System.Exception(Ex.Message);
}
}
public void InitLine()
{
this._lRenderer = GetComponent<LineRenderer>();
this._lRenderer.positionCount = 0;
this._lRenderer.startWidth = 0.15f;
this._lRenderer.endWidth = 0.15f;
this._lRenderer.useWorldSpace = true;
this._lRenderer.numCapVertices = 50;
}
}