- Home /
Null Reference in Line 0
Can someone Help me finding this null reference? im getting this in my Debug in Visual Studio (HoloLens):
NullReferenceException: Object reference not set to an instance of an object.
at Degree.Update()
at Degree.$Invoke7(Int64 instance, Int64* args)
at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method)
(Filename: <Unknown> Line: 0)
i have 5 buttons and every button has this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Degree : MonoBehaviour {
public EV3Control legocontrol;
public int degree;
public Texture mouseover;
public Texture option;
public Texture selected;
public GazeGestureManager Gazemanager;
// Select Button
void OnSelect()
{
legocontrol.DriveDegree(degree);
}
// Use this for initialization
void Start () {
Debug.Log(this.name);
}
// Update is called once per frame
void Update () {
int currentdegree = legocontrol.currentdegree;
//Debug.Log("EV3: " + legocontrol);
if (currentdegree == degree) {
//Debug.Log("Active: " + this.name);
try
{
this.GetComponent<Renderer>().material.mainTexture = selected;
}
catch {
Debug.Log("catch active: " + this.name);
}
}
if (currentdegree != degree)
{
if (Gazemanager.FocusedObject.name == this.name)
{
//Debug.Log("Name: " + this.name);
try
{
this.GetComponent<Renderer>().material.mainTexture = mouseover;
}
catch {
Debug.Log("catch focus: " + this.name);
}
}
else {
//Debug.Log("Else: " + this.name);
try
{
this.GetComponent<Renderer>().material.mainTexture = option;
}
catch {
Debug.Log("catch no: " + this.name);
}
}
}
}
}
no catch is thrown. how yould i find the problem? or is the whole script in wrong place? There is no Problem in functionality, only errors in debugger.
Uncommenting everything and placing following scripf throws NullReference.
Debug.Log("Gaze Name: " + Gazemanager.FocusedObject.name);
Debug.Log("This Name: " + this.name);
this.name throws null?
UPDATE: oh, stupid: this is reference to script, not the GameObject its attached to.
Answer by gjf · Feb 20, 2017 at 05:51 PM
please try to format ALL code - it makes the line numbers from errors match up ;)
it's likely that legocontrol
is null
- you should add a Debug.Log()
to see if that's the case... then only access currentdegree
if it's not.
it's a good idea to the get the reference to the renderer/material in Awake()/Start()
too... those operations are expensive.
Hi, i checked legocontrol which seems never to be null. ins$$anonymous$$d i found that this.name and Gazemanager.FocusedObject.name is null when they are out of sight. im wondering about this.name very much.
Debug.Log("Gaze Name: " + Gazemanager.FocusedObject.name);
Debug.Log("This Name: " + this.name);
Your answer
![](https://koobas.hobune.stream/wayback/20220612111532im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why am i getting a NullReferenceException when i switch platform to Android? 0 Answers
Object reference is NULL when IT IS set to an instance of an object? 1 Answer
Null Reference Exception Error. Modified Stats. Need Help. 1 Answer
NullReferenceException: Object reference not set to an instance of an object ProgressBar.Start () 2 Answers