• 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
Question by PlasmaYoshi · Oct 03, 2015 at 03:47 PM · collectible-game-objects

How can I make collectibles have to be picked up in a certain order?

I'm working on my first game. I'm trying to make an educational game for kids and toddlers about the ABC's, colors, and shapes. I want to make the collectibles/letters only be able to get picked up in a certain order. Please help. Thanks in advance. :D

Comment

People who like this

0 Show 0
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

Answer by MerryAce123 · Oct 03, 2015 at 06:02 PM

This can be done in many different ways however the best one is probably assign some index to your collectibles which will define the order. So lets say you do a script with one public variable called index so you can set it from the inspector. Then you assign it to you collectibles, so for example A gets index 0, B gets 1, C gets 2 etc. And then you put another script to you player/controller which will have an int variable called something like lastIndex and its default value will be 0. Every time your player/controller tries to collect something you ask if the index of an object you collided with is equal to your lastIndex variable + 1. If it is, then you can collect it and add 1 to your lastIndex variable.

Comment
Suddoha

People who like this

1 Show 4 · 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 PlasmaYoshi · Oct 03, 2015 at 06:09 PM 0
Share

Thank you! I'm going to try this out, and if I have any problems, tell you. Also, are you able to give me an example as a script, because I only started about 3 days ago.

avatar image Suddoha PlasmaYoshi · Oct 03, 2015 at 07:00 PM 0
Share

Note, that this attempt may be error prone but that's rather due to the nature of the problem itself. You may want to add a logic that checks that indices are not used twice and that all indices from 0 to n exist.

There are probably less error-prone ways of doing that, but here's what MerryAce123 was talking about:

He meant something like:

 // attach this to all collectables
 public class Example : MonoBehaviour
 {
     // assign in inspector
     [SerializeField]
     private int index;
     public int Index { get { return index; } }
 
     void Awake()
     {
         // let's just assign the tag via script
         // tag must exist in the Tag list otherwise this will fail and throw errors
         tag = "Collectable";
     }
 }

Note, that i made the actual index private in order to not let other scripts manipulate it and mess up the indices, so when you need to read it use the property 'Index'. 'SerializeField' will yet make it visible and editable in the inspector.

In order to not be forced to set the tag manually for each object you put into the scene, this script will also assign the tag. That's up to your desire, you can remove that.

The next script, which should only exist once in the scene is the following. Any further logic is simple to add, such as 'everything collected' or a counter for max attempts etc.

 public class Test : MonoBehaviour
 {
     private int nextIndex = 0;
 
     void Update()
     {
         // if the left (0) mouse button is clicked
         if(Input.GetMouseButtonDown(0))
         {
             RaycastHit hit;
             // create a ray that casts from mouse position
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
             // do the raycast, result will be stored in hit
             if (Physics.Raycast(ray, out hit))
             {
                 // just to have shorter syntax
                 GameObject go = hit.collider.gameObject;
 
                 // compare the tag
                 if(go.CompareTag("Collectable"))
                 {
                     // get the Example component and compare the index to the index we're waiting for
                     if (go.GetComponent<Example>().Index == nextIndex)
                     {
                         // logic when it's the correct item
                         Debug.Log("correct item");
                         nextIndex++;
                         // let's set the item to be inactive
                         go.SetActive(false);
                     }
                     else
                     {
                         // logic when it's the wrong item
                         Debug.Log("wrong item");
                     }
                 }
             }
         }
     }
 }
avatar image PlasmaYoshi Suddoha · Oct 06, 2015 at 03:05 AM 0
Share

I can't seem to get it working... I'm still a beginner. If I gave you the code, can you do the coding?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Collecting items and store them in memory to place them later at different locations. 1 Answer

How to show the effect when the coins are collected moves to the coin meter? 3 Answers

collectable currency doesn't transfer between different scenes 0 Answers

How do I make an object appear on screen after it is collected? 1 Answer

Multiple score values 1 Answer


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