NullReferenceException: Object reference not set to an instance of an object [Please Help]
The Error happendes when the "Day2" method is activated
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TextController : MonoBehaviour {
public Text text;
public Image imageGO;
float timeLeft = 3.5f;
private enum States {backstory, cell_1, scout_1, scout_2, talk_1, sleep_1, lock_1, blood_1};
private States myState;
void Start () {
myState = States.backstory;
}
void Update () {
if (myState == States.backstory) {
state_backstory();
}else if (myState == States.cell_1) {
state_cell_1();
}else if (myState == States.sleep_1) {
state_sleep_1();
}else if (myState == States.talk_1) {
state_talk_1();
}else if (myState == States.scout_1) {
timeLeft -= Time.deltaTime;
if(timeLeft < 0) {
myState = States.scout_2;
}
state_scout_1();
}else if (myState == States.scout_2) {
state_scout_2();
}else if (myState == States.lock_1) {
state_lock_1();
}else if (myState == States.blood_1) {
state_blood_1();
}
}
void state_backstory () {
text.text="You wake up in an odd bed, you quickly realize that you are in a prison cell." +
"You try to remember what you did that caused your arrest. But you could not recall a thing." +
"You try to remember who you even are, but you couldn’t, no matter how hard you tried.\n\n" +
"Press Enter to continue";
if (Input.GetKeyDown(KeyCode.Return)){
myState = States.cell_1;
}
}
void state_cell_1 () {
text.text="You hear people whom you assume are guards outside the room. what will you do?\n\n" +
"S.go to sleep, maybe you just need a rest and you’ll remember?\n" +
"T.Ask the guards why you’re there\n" +
"V.Scout the room maybe you could find some clues?";
if (Input.GetKeyDown(KeyCode.S)){
myState = States.sleep_1;
}else if (Input.GetKeyDown(KeyCode.T)){
myState = States.talk_1;
}else if (Input.GetKeyDown(KeyCode.V)){
myState = States.scout_1;
}
}
void state_sleep_1 () {
text.text="a day passes...";
Day_2();
}
void state_talk_1 () {
text.text="You ask the guards why you’re there.\n" +
"They laugh at you… it seems this isn’t going anywhere.\n" +
"The guards leave, now that you’re on your own, What should you do?";
if (Input.GetKeyDown(KeyCode.V)) {
myState = States.scout_1;
}else if (Input.GetKeyDown(KeyCode.L)) {
myState = States.lock_1;
}
}
void state_scout_1 () {
text.text="You look around the room…\n";
}
void state_scout_2 () {
text.text="you see some writing on the wall:”It never ends” and some scratches counting up to 20";
if (Input.GetKeyDown(KeyCode.V)) {
myState = States.blood_1;
}else if (Input.GetKeyDown(KeyCode.S)) {
myState = States.scout_1;
}
}
void state_lock_1 () {
}
void state_blood_1 () {
text.text="111";
}
void Day_2() {
imageGO.sprite = Resources.Load<Sprite> ("DayCounter13");
}
}
Answer by Chefsu · Mar 24, 2017 at 12:19 PM
public Image imageGO;
Did you assign an Image to this field in the editor? If that didn't do it, check if the name of the sprite is the same as in your Resources folder.
imageGO.sprite = Resources.Load<Sprite> ("DayCounter13");
If that didn't do it, check if you set the Texture Type in the inspector to: Sprite (2D and UI)
I hope this helped you out. If all above didn't work, click on the error and select everything that pops up beneath the console lines and paste it here.
Answer by idohadar111 · Mar 24, 2017 at 12:32 PM
NullReferenceException: Object reference not set to an instance of an object TextController.Day_2 () (at Assets/Scripts/TextController.cs:109) TextController.state_sleep_1 () (at Assets/Scripts/TextController.cs:68) TextController.Update () (at Assets/Scripts/TextController.cs:24)
There is nothing wrong with your code, the problem should be one of the things i said before.
Your answer
![](https://koobas.hobune.stream/wayback/20220612113844im_/https://answers.unity.com/themes/thub/images/avi.jpg)