I am making multiplication sum where int a and int b both are getting values from random.range(1,11). and there are 4 buttons to click correct answer. when correct answer button is clicked i am calling sum creator function again so next sum will appear. My problem is some times i get same question next time. for example first time 2 X 5 and when i recall sum creator again i get 2 x 5. i tried adding value of b to a list then comparing if the value is used earlier if yes then again randomize it.( i am trying to control only duplication of b) But as per my understanding I am assigning single value to b when i call sum creator function. there is no loop because i want only one question at a time. and when i recall the same function it does not remember which value was used previously. How do i solve this? please help.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class MathsAndAnswerScript : MonoBehaviour
{
public static MathsAndAnswerScript instance;
private int a, b;
//the variable for answer value
[HideInInspector] public int answer;
//varible whihc will assign ans to any one of the 4 answer button
private int locationOfAnswer;
//ref to the button
public GameObject[] ansButtons;
//get the tag of button
public string tagOfButton;
//ref to text in scene where we will assign a and b values of question
public TextMeshProUGUI valueA, valueB;
public static bool roundActive = false;
void Awake()
{
MakeInstance();
}
//method whihc make this object instance
void MakeInstance()
{
if (instance == null)
{
instance = this;
}
}
void Start()
{
tagOfButton = locationOfAnswer.ToString();
SumCreator();
}
void Update()
{
tagOfButton = locationOfAnswer.ToString();
}
void SumCreator()
{
// we do multiplication here
bool reloop;
bool[] numbers = new bool[301];
List<int> usedValues = new List<int>();
a = Random.Range(1, 11);
b = Random.Range(1, 11);
while (usedValues.Contains(b))
{
b = Random.Range(1, 11);
}
locationOfAnswer = Random.Range(0, ansButtons.Length);
answer = a * b;
numbers[answer] = true;
if (valueA != null && valueB != null)
{
valueA.text = a.ToString();
valueB.text = b.ToString();
}
// mathSymbolObject.sprite = mathSymbols[0];
for (int i = 0; i < ansButtons.Length; i++)
{
if (i == locationOfAnswer)
{
ansButtons[i].GetComponentInChildren<TextMeshProUGUI>().text = "" + answer;
}
else
{
// the below code make sure that all the values assigned to the ans button are within the range
int value = 0;
do
{
reloop = false;
if (answer <= 10)
{
value = Random.Range(0, 15);
}
else if (answer <= 30 & answer >= 11)
{
value = Random.Range(10, 45);
}
else if (answer <= 60 & answer >= 31)
{
value = Random.Range(25, 75);
}
else if (answer <= 90 & answer >= 61)
{
value = Random.Range(55, 105);
}
else if (answer <= 120 & answer >= 91)
{
value = Random.Range(85, 135);
}
if (numbers[value]) //already select?
{
reloop = true;
}
} while (reloop);
numbers[value] = true;
ansButtons[i].GetComponentInChildren<TextMeshProUGUI>().text = "" + value;
}
}//for loop
}
Button Script.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CheckButtonPress : MonoBehaviour {
private Button thisButton;
void Start()
{
//we get the button attached to the object
thisButton = GetComponent<Button>();
}
//method whihc help us to identify if player has pressed correct or wrong answer
public void checkTheTextofButton()
{
if (gameObject.CompareTag( MathsAndAnswerScript.instance.tagOfButton))
{
// do something
}
else
{
//do something else
}
//after we have answered the question we call the sum creator method to create new question
MathsAndAnswerScript.instance.SumCreator();
}
Aucun commentaire:
Enregistrer un commentaire