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.