Changing Text of multiple Button if Pressed Button Text is equal to UI Text
I write that code Butt it is not working plz help me ... i am new developer on unity
using UnityEngine; using System.Collections; using UnityEngine.UI; using System;
public class Guess : MonoBehaviour { public Text mainText; public Button b1; public Button b2; public Button b3; public Button b4; public Button b5; public Button b6; public Button b7; public Button b8;
String[] array;
void Start () {
mainText = mainText.GetComponent<Text>();
b1 = b1.GetComponent<Button>();
b2 = b1.GetComponent<Button>();
b3 = b1.GetComponent<Button>();
b4 = b1.GetComponent<Button>();
b5 = b1.GetComponent<Button>();
b6 = b1.GetComponent<Button>();
b7 = b1.GetComponent<Button>();
b8 = b1.GetComponent<Button>();
array = new string[8];
array[0] = "Pakistan";
array[1] = "India";
array[2] = "Amarica";
array[3] = "England";
array[4] = "Room";
array[5] = "Itly";
array[6] = "Turky";
array[7] = "Rausia";
System.Random rn = new System.Random();
int answer = rn.Next(8 ) + 1;
mainText.text = array[answer];
}
// Update is called once per frame
void Update () {
}
public void buttonRight1()
{
// String s = mainText.text;
/*
//suffle(s);
if (b1.GetComponentInChildren<Text>().text == s)
{
check();
}
*/
if (b1.GetComponentInChildren<Text>().text == mainText.text)
{
b1.GetComponentInChildren<Text>().text = randomName();
check();
}
}
public void buttonRight2()
{
//String s = mainText.text;
if (b2.GetComponentInChildren<Text>().text == mainText.text)
{
//b2.GetComponentInChildren<Text>().text = randomName();
b2.GetComponentInChildren<Text>().text = randomName();
check();
}
}
public void buttonRight3()
{
// String s = mainText.text;
if (b3.GetComponentInChildren<Text>().text == mainText.text)
{
b3.GetComponentInChildren<Text>().text = randomName();
check();
}
}
public void buttonRight4()
{
// String s = mainText.text;
if (b4.GetComponentInChildren<Text>().text == mainText.text)
{
b4.GetComponentInChildren<Text>().text = randomName();
check();
}
}
public void buttonLeft1()
{
//String s = mainText.text;
if (b5.GetComponentInChildren<Text>().text == mainText.text)
{
b5.GetComponentInChildren<Text>().text = randomName();
check();
}
}
public void buttonLeft2()
{
// String s = mainText.text;
if (b6.GetComponentInChildren<Text>().text == mainText.text)
{
b6.GetComponentInChildren<Text>().text = randomName();
check();
}
}
public void buttonLeft3()
{
//String s = mainText.text;
if (b7.GetComponentInChildren<Text>().text == mainText.text)
{
b7.GetComponentInChildren<Text>().text = randomName();
check();
}
}
public void buttonLeft4()
{
// String s = mainText.text;
if (b8.GetComponentInChildren<Text>().text == mainText.text)
{
b8.GetComponentInChildren<Text>().text = randomName();
check();
}
}
/*
public void suffle( String s )
{
if (b1.GetComponentInChildren<Text>().text == s)
{
check();
}
else if (b2.GetComponentInChildren<Text>().text == s)
{
check();
}
else if (b3.GetComponentInChildren<Text>().text == s)
{
check();
}
else if (b4.GetComponentInChildren<Text>().text == s)
{
check();
}
else if (b5.GetComponentInChildren<Text>().text == s)
{
check();
}
else if (b6.GetComponentInChildren<Text>().text == s )
{
check();
}
else if (b7.GetComponentInChildren<Text>().text == s)
{
check();
}
else if (b8.GetComponentInChildren<Text>().text == s)
{
check();
}
}
*/
public String randomName()
{
array[0] = "Pakistan";
array[1] = "India";
array[2] = "Amarica";
array[3] = "England";
array[4] = "Room";
array[5] = "Itly";
array[6] = "Turky";
array[7] = "Rausia";
System.Random rn = new System.Random();
int answer = rn.Next(8 ) + 1;
return array[answer];
}
public void check()
{
while (b1.GetComponentInChildren<Text>().text == b2.GetComponentInChildren<Text>().text
/* &&
b2.GetComponentInChildren<Text>().text == b3.GetComponentInChildren<Text>().text
&&
b3.GetComponentInChildren<Text>().text == b4.GetComponentInChildren<Text>().text
&&
b4.GetComponentInChildren<Text>().text == b5.GetComponentInChildren<Text>().text
&&
b5.GetComponentInChildren<Text>().text == b6.GetComponentInChildren<Text>().text
&&
b6.GetComponentInChildren<Text>().text == b7.GetComponentInChildren<Text>().text
&&
b7.GetComponentInChildren<Text>().text == b8.GetComponentInChildren<Text>().text
&&
b8.GetComponentInChildren<Text>().text == b1.GetComponentInChildren<Text>().text
*/
)
{
b1.GetComponentInChildren<Text>().text = randomName();
b2.GetComponentInChildren<Text>().text = randomName();
b3.GetComponentInChildren<Text>().text = randomName();
b4.GetComponentInChildren<Text>().text = randomName();
b5.GetComponentInChildren<Text>().text = randomName();
b6.GetComponentInChildren<Text>().text = randomName();
b7.GetComponentInChildren<Text>().text = randomName();
b8.GetComponentInChildren<Text>().text = randomName();
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Changing Sprites in Code (c#) help 0 Answers
Changing Player Position when hit by collider 0 Answers
Help with Changing Sprites in Code (c#) 0 Answers
position the player on the same position as an object 0 Answers
Material Changing c# 0 Answers