Is there an easier way to do this?
Hello,
I am making a scene with int 1 and int 2 as possible answers, to questions I've asked in a previous scene. Explanation of one of the answers gameobjects 1. Created the gameobject (that consists of UI images) with two possible answers - 1 and 2. They are integers. 2. Created 2 gameobjects (consits of UI image of a tick) and set them as un-active. Attached them to the parent gameobject(the answers).
If the player answers with the first answer he sets the playerprefs to interger 1. And if he answers with the second answer he sets the playerprefs to integer 2.
Here is the code, it's easier to understand form it:
using UnityEngine;
using System.Collections;
public class History : MonoBehaviour {
public GameObject t_1_A;
public GameObject t_1_B;
public GameObject t_2_A;
public GameObject t_2_B;
public GameObject t_3_A;
public GameObject t_3_B; // ----------> these are the answers(ticks)
public GameObject t_4_A;
public GameObject t_4_B;
public GameObject t_5_A;
public GameObject t_5_B;
int question1;
int question2;
int question3; //from here we set int to get the playerprefs
int question4;
int question5;
void Start () {
question1 = PlayerPrefs.GetInt("Question1");
question2 = PlayerPrefs.GetInt("Question2");
question3 = PlayerPrefs.GetInt("Question3"); // we get the playerprefs
question4 = PlayerPrefs.GetInt("Question4");
question5 = PlayerPrefs.GetInt("Question5");
if(question1 == 1){
t_1_A.SetActive(true);
t_1_B.SetActive(false);
}
else if(question1 == 2){
t_1_A.SetActive(false);
t_1_B.SetActive(true);
}
if(question2 == 1){
t_2_A.SetActive(true);
t_2_B.SetActive(false);
}
else if(question2 == 2){
t_2_A.SetActive(false);
t_2_B.SetActive(true);
}
if(question3 == 1){
t_3_A.SetActive(true);
t_3_B.SetActive(false);
}
else if(question3 == 2){
t_3_A.SetActive(false);
t_3_B.SetActive(true);
}
if(question4 == 1){
t_4_A.SetActive(true);
t_4_B.SetActive(false);
}
else if(question4 == 2){
t_4_A.SetActive(false);
t_4_B.SetActive(true);
}
if(question5 == 1){
t_5_A.SetActive(true);
t_5_B.SetActive(false);
}
else if(question5 == 2){
t_5_A.SetActive(false);
t_5_B.SetActive(true);
}
}
}
Now i have to add like 20-30 more answers to questions asked in the first scene of the game, and it gets messy. I would be grateful if some more skillful programmer helps me out here.
Thank you.
Answer by Pharaoh_ · Apr 26, 2016 at 10:49 PM
If there are always 2 answers to your questions, you can do this:
public class History : MonoBehaviour {
public GameObject[] answers;
int[] questions;
void Start () {
int newLength = answers.Length/2;
questions = new int [newLength];
for (int i = 0; i < newLength; i++) {
questions [i] = PlayerPrefs.GetInt ("Question" + i.ToString());
if (questions [i] == 1 || questions [i] == 2) {
if (questions [i] == 1) {
answers [i].SetActive (true);
answers [i + 1].SetActive (false);
} else {
answers [i].SetActive (false);
answers [i + 1].SetActive (true);
}
}
}
}
}
Could potentially be optimized further with a mod(ulo).
Thanks a lot! I can fully understand the code, although I could never come up with it myself! What's weird is that it's supposed to work, and it does to some degree, but what happens is this: The first half of the answers have both the ticks activated, and the second half of the answers have none


Answer by Stoyanow · Apr 28, 2016 at 10:10 AM
If anyone stumbles on this, I am still trying to fix the issue.
Your answer
Follow this Question
Related Questions
Button calls itself numerous times 0 Answers
How to carry over button sound to another scene 1 Answer
unable to attack enemy objects? 1 Answer
IndexOutOfRangeExeption! This doesent even affect my gameplay, it just is there to be annoying! 1 Answer
NullReferenceException: Error from the GetStyle() - Method 1 Answer