- Home /
Collecting Array items in order
I am setting up my avatar to collect an array of items that change location (which is done), but I'm trying to figure out a way to give a winning confirmation print after the 3 items (GameObjects) are triggered in order. The only way I see this working is possibly a boolean, but I don't know how I should start. Here is what I have so far in Javascript:
For the avatar's script:
public var items : GameObject[];
function Start () {
items = GameObject.FindGameObjectsWithTag("item");
for(var i : int = 0; i < items.Length; i++)
{
Debug.Log("Item "+i+" is named "+items[i].name);
}
}
For the item script:
var avatar;
avatar = GameObject.FindWithTag("avatar");
function Start(){
}
function OnTriggerEnter (other:Collider)
{
//changing the range of item positions when collided with
this.transform.position.x = Random.Range(-5.555938,4.46);
this.transform.position.z = Random.Range(-3.636441,3.63);
}
Answer by Anxo · Oct 28, 2014 at 04:06 PM
Give each type of object a varible, Bannanas = b , Apples = a, totamtos = t. If you need the player to collect 3 tomattos and one bananna, then one tomatto and THEN one apple, the incoming string would be
tttbta
You can just check against the string
var collectedCode : string = "";
function OnCollectItem(item: Item){
collectedCode += Item.code;
CheckCombination();
}
function CheckCombination(){
switch(collectedCode){
case(tttbta):
print("you collected 3 tomatos 1 banna, 1 tomato and 1 apple in the right order!");
break;
}
}
WUCC
@Anxo I tried your suggestion, but I'm getting errors about a ")" missing. $$anonymous$$aybe my code from your suggestion will help you understand why this is so:
var item = GameObject.Find("Sphere");
var item2 = GameObject.Find("Cylinder");
var item3 = GameObject.Find("Cube");
var collectedCode : string = "Sphere, Cylinder, Cube";
function OnCollectItem(item: Item){
collectedCode += Item.code;
CheckCombination();
}
function CheckCombination(){
switch(collectedCode){
case(item, item2, item3):
print("you collected 3 items in the right order!");
break;
}
}
Damit, wrote a long reply but in the answer and could not answer a second time so lost it. Ok here we go.
First problem is that in your OnCollectItem you are passing an Item into a variable called item but in the function you are not referencing the variable but the static class itself. So
collectedCode += Item.code;
// should be
collectedCode += item.code;
However, the outcome of collecting a sphere would then be something like "Sphere, Cylinder, CudeSphere" which is not what you want. Use Debug.Log or print in the end of the OnCollectItem function to see what you are doing. Also you want to keep your case singular with a single true or false statement. This is why I seperated the collectedCode from the acceptable code.
var collectedCode : string = "";
var acceptableCode : string = "SphereCylinderCube";
case(collectedCode == acceptableCode):
So far, it's not working and wondering if you wanted me to make a boolean when you to make a true or false statement.
Here's what I updated:
var item = GameObject.Find("Sphere");
var item2 = GameObject.Find("Cylinder");
var item3 = GameObject.Find("Cube");
var collectedCode : String = "";
var acceptableCode : String = "SphereCylinderCube";
function OnCollectItem(item: Item){
collectedCode += item.code;
CheckCombination();
}
function CheckCombination(){
switch(collectedCode){
case(collectedCode == acceptableCode):
print("you collected 3 items in the right order!");
break;
}
}
why not use item indexing? this is not a very expandable method.
although that is a good way to simplify the amount of items, I still don't have a clue how to declare if the order of the items are right or not.