Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CSmitten88 · Oct 27, 2014 at 10:58 PM · collisiongameobjectarraytriggerboolean

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);
    }




Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CSmitten88 · Oct 29, 2014 at 03:37 PM 0
Share

@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;
   }
 }
avatar image Anxo · Oct 29, 2014 at 03:56 PM 0
Share

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):
avatar image CSmitten88 · Oct 29, 2014 at 05:04 PM 0
Share

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;
  }
 }
avatar image bubzy · Oct 29, 2014 at 05:21 PM 0
Share

why not use item indexing? this is not a very expandable method.

avatar image CSmitten88 · Oct 29, 2014 at 05:26 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

If gameObject's center passes through a trigger? 1 Answer

trying to define the last object I collided with and store it as a variable? 2 Answers

Detecting if object in array collides with another object in same array 1 Answer

Storing objects in arrays 2 Answers

Alternate between two Audio clips on collision 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges