- Home /
Object reference not set to an instance of an object?
Hello,
I get this error but can't find a solution.
NullReferenceException: Object reference not set to an instance of an object UnityEngine.GUI.DrawTexture (Rect position, UnityEngine.Texture image, ScaleMode scaleMode, Boolean alphaBlend, Single imageAspect) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/GUI.cs:119) UnityEngine.GUI.DrawTexture (Rect position, UnityEngine.Texture image) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/GUI.cs:116) Scoreteller.Score () (at Assets/Scripts/Scoreteller.cs:64) Scoreteller.Update () (at Assets/Scripts/Scoreteller.cs:92)
using UnityEngine; using System.Collections;
public class Scoreteller : MonoBehaviour { public Texture2D score0; public Texture2D score1; public Texture2D score2; public Texture2D score3; public Texture2D score4; public Texture2D score5; public Texture2D score6; public Texture2D score7; public Texture2D score8; public Texture2D score9; private Rect score1tientallenRect; private Rect score1eenhedenRect; private Rect score2tientallenRect; private Rect score2eenhedenRect; string reactie; // Use this for initialization void Start () { score1tientallenRect = new Rect(Screen.width*0.17f, Screen.height*0.08f,Screen.width 0.04f,Screen.height*0.08f); score1eenhedenRect = new Rect(Screen.width*0.21f, Screen.height*0.08f,Screen.width 0.04f,Screen.height*0.08f); score2tientallenRect = new Rect(Screen.width*0.74f, Screen.height*0.08f,Screen.width 0.04f,Screen.height*0.08f); score2eenhedenRect = new Rect(Screen.width*0.78f, Screen.height*0.08f,Screen.width 0.04f,Screen.height*0.08f); } public void Score() { for(int j=0;j<Quiz.Spelers.Count;j++) { string scorestring = Quiz.Spelers[j].Score.ToString(); if(scorestring.Length == 1) { scorestring = "0"+scorestring; } for(int i=0;i<scorestring.Length;i++) { int test = int.Parse(scorestring[i].ToString()); Rect testRect; if(j==0) { if(i==0) { testRect = score1tientallenRect; } else { testRect = score1eenhedenRect; } } else { if(i==0) { testRect = score2tientallenRect; } else { testRect = score2eenhedenRect; } } switch (test) { case 0: GUI.DrawTexture(testRect, score0); break; case 1: GUI.DrawTexture(testRect, score1); break; case 2: GUI.DrawTexture(testRect, score2); break; case 3: GUI.DrawTexture(testRect, score3); break; case 4: GUI.DrawTexture(testRect, score4); break; case 5: GUI.DrawTexture(testRect, score5); break; case 6: GUI.DrawTexture(testRect, score6); break; case 7: GUI.DrawTexture(testRect, score7); break; case 8: GUI.DrawTexture(testRect, score8); break; case 9: GUI.DrawTexture(testRect, score9); break; default: reactie = "Er is iets fout gegaan "; break; } } } } // Update is called once per frame void Update () { Score(); } }
Answer by loopyllama · Mar 11, 2011 at 10:20 AM
You are calling GUI.DrawTexture which requires it to be in an OnGUI() function. Check out the docs here: http://unity3d.com/support/documentation/Components/gui-Basics.html
Fix the whole thing by adding a:
void OnGUI() { //and cut paste your entire switch statement here }
Your answer
Follow this Question
Related Questions
How do I resize the Lightmap Array via Editor Script? 1 Answer
How can this possibly be null? 2 Answers
NullReferenceException After A Few Hours? 1 Answer
Object reference not set to an instace of an object (JS) 0 Answers
NullReferenceException: Object reference not set to an instance of an object 1 Answer