Applying a script to two objects not working
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.
CheckAns() should probably have a parameter for which gameobject to destroy.
i.e
CheckAns(Gameobject blockade)
Then it has a reference of which blockade to destroy when a question is answered correctly
if (col.gameObject.name == "Blockade")
{
QuestionPanel.SetActive(true);
Question$$anonymous$$aker();
CheckAns(col.gameObject);
}
Your Question$$anonymous$$aker is messed up, you should just cut
N1 = Random.Range(1, 10);
N2 = Random.Range(1, 10);
From GetRandomOperator and put it inside Question$$anonymous$$aker
You don't need to call GetRandomOperator directly after Question$$anonymous$$aker, as you already used GetRandomOperator to assign myOperator inside your Question$$anonymous$$aker
You can't put Gameobject blockade in the brackets
Plus the issue is that both blocks will trigger a question but only one can be destroyed. The same block is always destroyed no matter what block the question is answered on.
Answer by roman_sedition · Nov 15, 2016 at 04:47 AM
You need to put it in the function declaration of CheckAns, that way you can use a GameObject parameter for CheckAns()
public void CheckAns(GameObject blockade)
{
//your block of code
}
This way you pass whichever blockade is asking the question as an argument, if the answer is right it destroys the blockade which was passed as a GameObject Reference
Your answer
Follow this Question
Related Questions
Is calling OnCollisionEnter2D on allot of GameObjects leading to worse performance? 0 Answers
Can't make the player attack an enemy 0 Answers
Problems with raycasting 2D. Probelmas com raycasting 2D. 0 Answers
Checking if the player jumps while not grounded 0 Answers
How do I move a game object only on the x axis with arrow keys? 1 Answer