- Home /
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
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.
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.
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");
}
}
}
}
}
}
I can't seem to get it working... I'm still a beginner. If I gave you the code, can you do the coding?