[Help] Trying to change the Image on every state

In the Text 101 lecture, Ben covers how to change each text for the adventure game. I am trying to do the same, but to change an image for each state to accompany the text. I’m fairly certain that my entire script is incorrect, as I have just tried copying the TextController script, but modifying it to suit images.


[code]using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ImageController : MonoBehaviour {

public Image image;
private enum States {introimage_1, introimage_2, introimage_3, introimage_4, introimage_5, introimage_6, introimage_7, introimage_8, introimage_9, introimage_10, introimage_11};
private Image myImage;

// Use this for initialization
void Start () {
	myImage = Image.introimage_1;

// Update is called once per frame
void Update () {
	print (myImage);
	if (myImage == Image.introimage_1) {
	} else if (myImage == Image.introimage_2) {
void state_introimage_1 () {
	if (Input.GetKeyDown(KeyCode.Space)) {
		myImage = Image.introimage_2;


Now, with this script alone I get numerous errors on the console:

But if I add UnityEngine.UI.image into the relevant script I get this:

Desired Effect:
For script to correctly update the imageintro_1 to imageintro_2 on (Input.GetKeyDown(KeyCode.Space)).

Observed Effect:
A whole assortment of errors which I am unsure how to resolve. I’m sorry if this is considerably vague but it definitely has something to do with some part of the referred script.

Further Information:
I am using Unity 4.6.9f1

Hello Dean,

With my version of Text 101, I had an image change to reflect each state. The way I did it was to have a child object linked in the Canvas, it was an “Image” component.


Then in the Inspector I chose the default image for the sprite so it has something to show in the box otherwise it just a Filled in Square. I placed it on the canvas where I needed it. I renamed it ImageLinker1.

Then, in the top section of TextController.cs script, I setup a whole stack of public sprite images ala:

public Sprite prisonGuard; 
public Sprite prisonBed;
public Sprite sprPrisonInCloset;
etc (there were 14 in total)

There probably was a better way to do it, but, I was new to Unity and did it the “easiest” way I could.

Next, I linked the public vars with the actual sprite Images in the Inspector that I used throughout the game, you can drag and drop or choose the image from the list that came up, I choose the latter. I also declared the Name of the Image Box as a public variable as well in TextController.cs

public Image imageLinker;

Then used the Inspector to point it to the right component.

Next, was to change the actual Sprite in ImageLinker1. To do this I used one line of code in every state block like this:

imageLinker.sprite = sprPrisonInCloset;

And this changed the sprite in ImageLinker1 using the actual sprite as declared in the public variable.

I have left a few things out as I had a few different things in the canvas like a scrolling TextBox, another Image box, and a Slider added, because, I’m weird like that. But that’s basically what I did to get the Images changing.

I did start off with another script in mind like you were doing but I couldn’t figure out how to link the two things together without delving further into Unity and the State management was really starting to get out of hand. So, in the end, I did it as simply as I could.

Hope that helps in some small way, if not, just give me a yell.


The way I'd do it is make an Image Game Object array.
in the initialized variables section
public SpriteRenderer[] stateImage = new SpriteRenderer[number of Images]
int stateImageNumber = 0;
//have a number for each state picture. remember with arrays it starts from 0
//for first Image (presumably the cell)
stateImageNumber = 0;
void ImageEnabler(){
for(i = 0; i<stateImage.Length; i++)
    if (i == stateImageNumber)
stateImage[i].enabled = true
stateImage[i].enabled = false;
if(Input.GetKey(keyCode.UpArrow) )
stateImage = 2
myState = States.Mirror
stateImagenumber == 2

Privacy & Terms