# How do I make an image appear based on the current state I'm in?

I am following a tutorial called Complete C# Unity Developer 2D: Learn to Code Making Games. In one section we create a text-based game, where we display a canvas with some UI elements to the user. The text changes based on the current state, which is setup through a couple of scripts. Just for future knowledge (it isn't part of the tutorial), I want to make an image display for each state.

Before I post the scripts, here is what I have tried: 1) I serialized a Sprite field in one of the scripts so that I could assign a .jpg asset sprite to it. At that point I was not sure how to get it to appear based on the state, which is an array of states. 2) I created a UI Image and added it to the canvas, then disabled it. My intent here was to enable it when the state.name == x. Unfortunately, I can't seem to get that to work either.

I am using two scripts, 1) AdventureGame.cs & 2) State.cs:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

{
[SerializeField]
Text m_textComponent;
[SerializeField]
State startingState;
// I added this Sprite field
[SerializeField]
Sprite m_spriteComponent;

State state;

void Start()
{
state = startingState;
m_textComponent.text = state.GetStateStory();
// This is what I added, a call to a method in the other script
m_spriteComponent = state.GetStateSprite();
Debug.Log(m_spriteComponent);
// The sprite is called HellDoor
}

void Update()
{
ManageState();
}

private void ManageState()
{
var nextStates = state.GetNextStates();
for (int index = 0; index < nextStates.Length; index++)
{
if (Input.GetKeyDown(KeyCode.Alpha1 + index))
{
state = nextStates[index];
}
m_textComponent.text = state.GetStateStory();

}
// Tested for state name to be sure, Door is one of them

Debug.Log(state.name);
if (state.name == "Door")
{
// Not sure what to put here
}

}

}


2) State.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// Creates a new slot called State in the Create menu!
public class State : ScriptableObject
{
// First number determines minimum size of the field, and the second number
// determines the amount of lines before you start to scroll
[TextArea(10, 14)]
[SerializeField]
string m_storyText;
[SerializeField]
State[] nextStates;
// This is the field I added
[SerializeField]
Sprite m_stateSprite;

public string GetStateStory()
{
return m_storyText;
}

public State[] GetNextStates()
{
return nextStates;
}
// This is the method I added
public Sprite GetStateSprite()
{
return m_stateSprite;
}
}


Additionally, this is what I have setup in the editor.