In short, I’ve got these blocks which, when collided with, generate a random + or - problem e.g 2 - 5. Now I originally only had one of these blocks in the first level but other levels will need more than one. I have been working for hours trying to find a way to allow blockade 1 and 2 to be able to be affected by the script. Once the question is answered correctly, the blockade will be destroyed allowing the player to continue. Here is the code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Collisions : MonoBehaviour
{
public GM gm = new GM();
private int N1;
private int N2;
private int AnswerCorrect;
public Text inputfield;
public Text QuestionBox;
public GameObject QuestionPanel;
string opperand;
public GameObject Blockade;
public GameObject Blockade2;
public GameObject Enemy;
public KeyCode Submit;
void Update()
{
if (Input.GetKey(Submit))
{
CheckAns();
}
}
public void QuestionMaker()
{
string myOperator = GetRandomOperator();
QuestionBox.text = "What is " + N1 + myOperator + N2 + "?";
}
public string GetRandomOperator()
{
int randomOperator = Random.Range(0, 2);
N1 = Random.Range(1, 10);
N2 = Random.Range(1, 10);
switch (randomOperator)
{
case (0):
opperand = "-";
AnswerCorrect = N1 - N2;
break;
case (1):
opperand = "+";
AnswerCorrect = N1 + N2;
break;
}
return opperand;
}
public void CheckAns()
{
if (inputfield.text == AnswerCorrect.ToString())
{
Destroy(Blockade);
QuestionPanel.SetActive(false);
}
else
{
QuestionMaker();
}
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name == "End")
{
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.buildIndex + 1);
}
if (col.gameObject.name == "Blockade")
{
QuestionPanel.SetActive(true);
QuestionMaker();
GetRandomOperator();
CheckAns();
}
if (col.gameObject.name == "Blockade2")
{
QuestionPanel.SetActive(true);
QuestionMaker();
GetRandomOperator();
CheckAns();
}
}
}
I am well aware that this will not work because I removed all the things I tried so that whoever replies may have a crack at it. Thank you.