Destroying an object with a specific name
I am trying to create a platformer where you are able to answer questions and get points, the way i have done it is that when the player collides with a GameObject in my case a question mark a menu pops up asks you the question and then you answer it, and throughout the level there are many question marks so you can answer multiple questions and get more points as you progress. I am having trouble with deleting a question mark that I have already answered.
This class is attched to the question mark
public void OnTriggerEnter2D(Collider2D other)
{
if (isPaused)
{
Resume();
}
else if (other.gameObject.name == "Player")
{
questionMarkNumber = (this.gameObject.name);
Pause();
Debug.Log("collided with questionMark");
}
This is attached to the menu that pops up when the question is asked
void Update () {
if(randQuestion == -1)
{
randQuestion = Random.Range(0, 5);
}
if(randQuestion > -1)
{
question = GetComponent<Text>();
question.text = questions[randQuestion];
}
if (choiceSelected == "y")
{
choiceSelected = "n";
if (correctAnswer[randQuestion] == selectedAnswer)
{
Debug.Log("Correct!!" + " " + randQuestion);
QuestionMark.GetComponent<QuestionsMenu>().Resume();
GameMaster.Instance.AddPoints(20);
Destroy(GameObject.Find(QuestionMark.GetComponent<QuestionsMenu>().questionMarkNumber));
randQuestion = -1;
}
}
}
Atm it deletes the first question mark but then on the remaining question marks gives a MissingReferenceException since I can only reference the first QuestionMark in the second class, which gets deleted.