Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by bmbz2017 · Mar 17, 2017 at 11:17 PM · 2dplatformerpickupordering

Picking up 2D collectables in an order?

Hi Everyone,

I am creating a 2D platform game for children. Currently, the player can pick up objects in any order, however when the levels progress I wish for them to be picked up in a certain order.

An example of this would be: Not being able to pick up the orange block, before the green.

This my method for collecting objects. This is attached to the collectable game Objects within my game. This also checks that once all the items have been collected, then the next level can be loaded.

 void OnTriggerEnter2D(Collider2D other)
 {

     if (other.tag == "Player")
     {
         ++BlocksCollected;
         Debug.Log(BlocksCollected);
         gameObject.GetComponent<AudioSource>().Play();
         Collect();

         if (BlocksCollected == 4)
         {

             SceneManager.LoadScene("Level2");
         }

     }

 }

}

If anyone could help me or guide me on how to do this, I would appreciate it!

Thanks!

Comment
Add comment · Show 4
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 TreyH · Mar 18, 2017 at 12:22 AM 1
Share

Do the blocks have some sort of "BlockColor" enum-style property? If so, you could set up an array in the inspector to control the block order itself, then monitor the required next block that way?

You could consider adding something like this to the "player" doing the collecting:

Edit: be sure to build out the color array in your inspector.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public enum BlockType
 {
     Red = 0,
     Blue = 1,
     Yellow = 2,
     Green = 3,
     Black = 4,
     White = 5,
 }
 
 public class BlockGrabber : $$anonymous$$onoBehaviour {
 
     // Substitute your own enum for colors, this
     // will just show the idea.
     public BlockType[] colorOrder;
 
     // Then keep track of your progress
     private int nextColorIndex = 0;
 
     // Check if a given block type would satisfy
     // the order
     public bool $$anonymous$$atchesNextBlock( BlockType b)
     {
         // The index is only being incremented here,
         // so we shouldn't be getting any weird behavior
         //
         return b == this.colorOrder[this.nextColorIndex];
     }
 
     // Increment the index and see if that was  the last
     // color required
     public bool IncrementandCheck()
     {
         // Add to our index
         this.nextColorIndex++;
 
         // Then return whether this exceeds our color array
         return this.nextColorIndex >= this.colorOrder.Length;
     }
 }

Then adding a property with that enum onto your blocks themselves, adjusting the collision function appropriately:

 public BlockType blockType;
 
 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.tag == "Player")
     {
         // We'll check the array on this object
         BlockGrabber grabber = other.GetComponent<BlockGrabber>();
 
         // See if this block's color is what the player
         // wanted to grab next
         bool match = grabber.$$anonymous$$atchesNextBlock( this.blockType);
 
         // If this isn't the right block, break out here
         if (!match)
             return;
 
         // If we're still here, then this block fits the bill, so
         // check if we're actually done
         bool done = grabber.IncrementandCheck();
 
 //            ++BlocksCollected;
 //            Debug.Log(BlocksCollected);
 
         gameObject.GetComponent<AudioSource>().Play();
 
 //            Collect();
 
         if (done)
         {
             Scene$$anonymous$$anager.LoadScene("Level2");
         }
     }
 }
avatar image bmbz2017 TreyH · Mar 18, 2017 at 01:09 AM 0
Share

Hi @TreyH,

Thank you for your help! I now have added this into my code. However, I am getting the error 'array index is out of range'. Do you know why this may be?

Thanks again!

avatar image TreyH · Mar 18, 2017 at 01:34 AM 0
Share

The example array there will need to be populated within the inspector, but, if you've already done that, then the player might need to have their collection index reset on a new scene.

By default, inspector arrays are of length 0, so any index will throw an exception.

avatar image bmbz2017 TreyH · Mar 18, 2017 at 03:55 AM 1
Share

Thank-you so much I got it working! I didn't set the size of the array on the BlockGrabber.

Thanks again!

0 Replies

· Add your reply
  • Sort: 

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

133 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Checking if the player jumps while not grounded 0 Answers

"Is Kinematic" required? 1 Answer

Adding sprites to same layer as bone in 2D animation rig 1 Answer

Change angle of player when changing slope angles 0 Answers

2D Runtime Editor for Platformer 0 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