- Home /
How can I compare a sequence of GameObjects when tapped?
I've create a list of GameObjects A to Z. They're then picked randomly and displayed on a 2D grid.
Next, I want to be able to tap the GameObjects in sequence A to Z.
How can I compare the list of GameObjects to know that the sequence is correct when tapped?
Originally I tagged each alphabet prefab as "A" to "Z", but since I needed to count the length, I changed the tag to "Alphabets". However, I now cannot destroy the object based on tag A - Z, and am trying to use the object name instead. It doesn't seem to work.
Thank you.
void OnMouseDown()
{
GameObject[] alphabetCount;
alphabetCount = GameObject.FindGameObjectsWithTag("Alphabets");
if(alphabetCount.Length == 26){
if(this.object.name("A")){
{
Destroy (this.gameObject);
}
}
}
}
Just to note: Based on the general presentation for an object's name, it appears that your if statement might work if formatted as:
if(this.object.name == "A")
I would presume, from the way you assembled it in your example, that you might have originally been using CompareTag(), which would be the faster way of testing a tag's name, but is only relevant to using tags.
Answer by fafase · Jun 18, 2015 at 11:04 AM
every time you tap an object run the check:
GameObject[] original = new GameObject[]{ // Define your sequence};
List<GameObject>list = new List<GameObject>();
bool CheckSequence()
{
for(int i = 0; i < original.Length && i < list.Count; i ++){
if( original[i] != list[i] ) { return false; }
}
return true;
}
Now it also depends on what you are meant to check so you change the if section to what you need.
the CheckSequence gets call on touch of a game object.
void OnMouseDown()
{
bool result = CheckSequence();
Debug.Log("Sequence is " + result.ToString());
}
Thanks! I've been trying and I'm not too sure with the sequence part as I get an error.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Input$$anonymous$$anager : $$anonymous$$onoBehaviour {
GameObject[] original = new GameObject[]{"A","B","C","D","E","F","G","H","I","J","$$anonymous$$","L","$$anonymous$$","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
//Tile$$anonymous$$anager.alphabetTilesList;
//new GameObject[]{"A","B","C","D","E","F","G","H","I","J","$$anonymous$$","L","$$anonymous$$","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
List <GameObject> alphaCheckList = new List <GameObject>();
bool CheckSequence()
{
for(int i = 0; i < original.Length && i < alphaCheckList.Count; i ++){
if( original[i] != alphaCheckList[i] ) { return false; }
}
return true;
}
void On$$anonymous$$ouseDown()
{
bool result = CheckSequence();
Destroy (gameObject);
}
}
Actually I've created the list in another script
Tile$$anonymous$$anager.cs as
public List <GameObject> alphabetTilesList = new List <GameObject> (); //A list of alphabet tiles.
I tried using the list in the above example: GameObject[] original = Tile$$anonymous$$anager.alphabetTilesList; which I will then get a different error: error CS0236: A field initializer cannot reference the nonstatic field, method, or property `Tile$$anonymous$$anager.alphabetTilesList'
Which is the best way to solve this error?
GameObject []original ;
void Start(){
original = Tile$$anonymous$$anager.alphabetTilesList;
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Lasers wont stop firing?? 1 Answer
Particle System On Key Press 2 Answers
How do I make a prefab ignore collisions with another prefab temporarily. 2 Answers