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 /
avatar image
0
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
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
1

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
Add comment · 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 $$anonymous$$erryAce123 was talking about:

He meant something like:

 // attach this to all collectables
 public class Example : $$anonymous$$onoBehaviour
 {
     // 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 : $$anonymous$$onoBehaviour
 {
     private int nextIndex = 0;
 
     void Update()
     {
         // if the left (0) mouse button is clicked
         if(Input.Get$$anonymous$$ouseButtonDown(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.

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

in the project roll the ball while COLLECTING THE PICK UP OBJECTS the ball goes through and dosen't pick up the object 0 Answers

I have multiple collectibles that can be pressed to add to score. But how do I have it so a collectible can only count once to the score? 1 Answer

Player collects X amount of coins - Object is destroyed 2 Answers

How to place many or array of collectables with organized distance from each other and by doing it easy way? 0 Answers

On trigger Activate collect item type of objective then play a cutscene after all are collected at a trigger too. 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