Get Image from another scene

I am making a sort of text based game. In the main menu of the game, you get to select an avatar. This whole avatar system does not run on databases or lists. Its kind of setactive this image when int = some number. and hides the rest. I also created a bool for every image. It becomes true when an particular image is clicked and becomes false when another image is active. I have another scene named main game. I want the selected avatar from the main menu scene to main game scene.

Here’s the script of the avatar selection which is on the main menu.

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

public class avatarselection : MonoBehaviour {

	public int numberOfItems =1;
	public GameObject image1go, image2go, image3go, image4go, image5go;

static public bool img1;
	static public bool img2;
	static public bool img3;
	static public bool img4;
	static public bool img5;

void Start () 
	{
		image1go.SetActive (false);
		image2go.SetActive (false);
		image3go.SetActive (false);
		image4go.SetActive (false);
		image5go.SetActive (false);
		//defaulImage.SetActive (false);
	}

void Update () 
	{
		



		if (numberOfItems == 1) 
		{
			//defaulImage.SetActive (false);
			image1go.SetActive (true);
			image2go.SetActive (false);
			image3go.SetActive (false);
			image4go.SetActive (false);
			image5go.SetActive (false);
			img1 = true;
			img2 = false;
			img3 = false;
			img4 = false;
			img5 = false;
		}
		if (numberOfItems == 2) 
		{
			//defaulImage.SetActive (false);
			image1go.SetActive (false);
			image2go.SetActive (true);
			image3go.SetActive (false);
			image4go.SetActive (false);
			image5go.SetActive (false);
			img1 = false;
			img2 = true;
			img3 = false;
			img4 = false;
			img5 = false;
		}
		if (numberOfItems == 3) 
		{
			//defaulImage.SetActive (false);
			image1go.SetActive (false);
			image2go.SetActive (false);
			image3go.SetActive (true);
			image4go.SetActive (false);
			image5go.SetActive (false);
			img1 = false;
			img2 = false;
			img3 = true;
			img4 = false;
			img5 = false;
		}
		if (numberOfItems == 4) 
		{
			//defaulImage.SetActive (false);
			image1go.SetActive (false);
			image2go.SetActive (false);
			image3go.SetActive (false);
			image4go.SetActive (true);
			image5go.SetActive (false);
			img1 = false;
			img2 = false;
			img3 = false;
			img4 = true;
			img5 = false;
		}
		if (numberOfItems == 5) 
		{
			//defaulImage.SetActive (false);
			image1go.SetActive (false);
			image2go.SetActive (false);
			image3go.SetActive (false);
			image4go.SetActive (false);
			image5go.SetActive (true);
			img1 = false;
			img2 = false;
			img3 = false;
			img4 = false;
			img5 = true;
		}
			
	}

	public void rightSelection()
	{
		numberOfItems += 1;
	}

	public void leftSelection()
	{
		numberOfItems -= 1;
	}
}

Now this is the avatarShow script in the scene main game. This gets the bool value from script above and enables image according to the bool.

using UnityEngine;
using System.Collections;

public class avatarshow : MonoBehaviour {

	public GameObject image1, image2, image3, image4, image5;



	// Use this for initialization
	void Start () 
	{
		image1.SetActive (false);
		image2.SetActive (false);
		image3.SetActive (false);
		image4.SetActive (false);
		image5.SetActive (false);
	}
	
	// Update is called once per frame
	void Update () 
	{
		if (avatarselection.img1 == true) 
		{
			image1.SetActive (true);
		}
		if (avatarselection.img2 == true) 
		{
			image2.SetActive (true);
		}
		if (avatarselection.img3 == true) 
		{
			image3.SetActive (true);
		}
		if (avatarselection.img4 == true) 
		{
			image4.SetActive (true);
		}
		if (avatarselection.img5 == true) 
		{
			image5.SetActive (true);
		}
	}
}

But this does not work. Plz Help.

First of all, you are in desperate need of some loops and overall optimizations.

Also, static members on monobehaviours are still set to null when the monobehaviour is destroyed, IE, new scene loaded.
So at the bottom is a class to store the info in. Also there is a lot better ways of doing this, but hopefully this points you in the right direction. Dynamically loading the image or texture is a good example of a better way.

This should be MUCH cleaner than your current code.

 public class avatarselection : MonoBehaviour
{
    public int NumberOfItems
    {
        get { return numberOfItems; }
        set { if (value != numberOfItems)
            {
                doSwitch = true;
            }
            numberOfItems = value;
            numberOfItems = Mathf.Clamp(numberOfItems, 0, images.Length);
            GameManager.avatarSelection = numberOfItems;
        }
    }
    private int numberOfItems;
    public GameObject[] images;
    public bool doSwitch;
    void Start()
    {
        for (int i = 0; i < images.Length; i++)
        {
            images*.SetActive(false);*

}
//defaulImage.SetActive (false);
}

void Update()
{
if (doSwitch)
{
for (int i = 0; i < images.Length; i++)
{
if (i == NumberOfItems)
{
images*.SetActive(true);*
}
else
{
images*.SetActive(false);*
}
}
doSwitch = false;
}
}
public void setSelection(bool direction)
{
if (direction)
{
NumberOfItems += 1;
}
else
{
NumberOfItems -= 1;
}
}
}

Now for your avatar class.
public class avatarshow : MonoBehaviour
{
public GameObject[] images;
void Start()
{
for (int i = 0; i < images.Length; i++)
{
if (GameManager.avatarSelection == i)
{
images*.SetActive(true);*
}
else
{
images*.SetActive(false);*
}
}
}
}
And finally where we ACTUALLY keep our reference
public static class GameManager
{
public static int avatarSelection;
}

To address retaining states from scene to scene, I would usually handle it via the singleton approach, where I have a gameobject that remains “Alive” between the scenes.

So the concept is that you’d make a public static instance of the object, and apply DontDestroyOnLoad to it. That way, the object remains Alive when the scene changes, and thus retains the necessary values you need carried over.

One thing to be aware of is, if you have references to objects in the previous scenes, those would become null as they got destroyed when the scene changed.

This is a good tutorial reference:
https://unity3d.com/learn/tutorials/projects/2d-roguelike-tutorial/writing-game-manager