Hi
I have just updated my unity and have now started to run into some errors with my code. The main issue is :
NullReferenceException: Object reference not set to an instance of an object Player.Update () (at Assets/Scripts/Player.cs:48)
I have read online that it might be due to the highliter component not being declerd but I havent had this issue with my code before it was updated, I have also tryed declareing it and that isnt the issue. If someone could point me in the right direction it would be greatfully apreacated.
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Player : MonoBehaviour {
HighlighterComponent activeHighlighterComponent;
[SerializeField]
DisplayBoxInitializer boxInitializer;
[SerializeField]
GameObject recticle;
int objectsSelected = 0;
bool isActive = false;
[SerializeField]
Text text;
// Use this for initialization
void Start () {
text.text = "Highlighting Active: " + isActive.ToString();
Screen.lockCursor = true;
recticle.SetActive(isActive);
}
// Update is called once per frame
void Update () {
Screen.lockCursor = true;
if(isActive)
{
Ray ray;
ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.forward, Color.green);
RaycastHit hitInfo;
if(Physics.Raycast(ray, out hitInfo ))
{
if(activeHighlighterComponent != null)
{
HighlighterComponent inframeHighlightComponent = hitInfo.collider.GetComponent<HighlighterComponent>();
if(activeHighlighterComponent != inframeHighlightComponent)
{
boxInitializer.UnInitialize();
activeHighlighterComponent.RemoveHighlight();
activeHighlighterComponent = hitInfo.collider.GetComponent<HighlighterComponent>();
if(activeHighlighterComponent == null)
{
activeHighlighterComponent = hitInfo.collider.gameObject.AddComponent<HighlighterComponent>();
}
activeHighlighterComponent.AddHighlight();
}
}
else
{
activeHighlighterComponent = hitInfo.collider.GetComponent<HighlighterComponent>();
if(activeHighlighterComponent == null)
{
activeHighlighterComponent = hitInfo.collider.gameObject.AddComponent<HighlighterComponent>();
}
activeHighlighterComponent.AddHighlight();
}
if((Input.GetMouseButtonUp(0) && !EventSystem.current.IsPointerOverGameObject()) || Input.GetButtonUp("Submit"))
{
boxInitializer.Initialize(activeHighlighterComponent.gameObject);
objectsSelected++;
}
}
if(Input.GetMouseButtonUp(1) || Input.GetButtonUp("Cancel"))
{
boxInitializer.UnInitialize();
}
}
if(Input.GetButtonUp("Edit Highlighting"))
{
isActive = !isActive;
if(!isActive)
{
boxInitializer.UnInitialize();
activeHighlighterComponent.RemoveHighlight();
activeHighlighterComponent = null;
}
text.text = "Highlighting Active: " + isActive.ToString();
recticle.SetActive(isActive);
}
}
void OnApplicationQuit()
{
//GoogleAnalytics.Client.SendEventHit("User Interaction", "Items Hit", "", objectsSelected);
//GoogleAnalytics.Client.SendEventHit("User", "Play Time", "", (int)Time.realtimeSinceStartup);
}
}