- Home /
Item collection in specific sequence.
Hi. I have a simple 3D level with three items to be collected. I want them to be collected in order, that is to say, I want Items #2 and #3 to be "locked" until the preceding item is collected.
Right now, if you follow the game's instructions and UI prompts, its exactly as I want. But if a player happens to find item #2 or #3, the game UI gets discombobulated, and it is just not how I want it.
If this has been answered previously, help me find the thread.
public class Collection : MonoBehaviour
{
public GameObject Prompt1;
public GameObject Prompt2;
public GameObject Prompt3;
public GameObject Prompt4;
// Start is called before the first frame update
void Start()
{
Prompt1.SetActive(true);
Prompt2.SetActive(false);
Prompt3.SetActive(false);
Prompt4.SetActive(false);
}
// Update is called once per frame
void OnTriggerEnter(Collider player)
{
Destroy(gameObject);
print("Picked Up Item");
if (gameObject.name == "potionBlue")
{
Prompt2.SetActive(true);
Prompt1.SetActive(false);
Prompt3.SetActive(false);
Prompt4.SetActive(false);
}
if (gameObject.name == "plateRound")
{
Prompt1.SetActive(false);
Prompt2.SetActive(false);
Prompt3.SetActive(true);
Prompt4.SetActive(false);
}
if (gameObject.name == "lotus")
{
Prompt1.SetActive(false);
Prompt2.SetActive(false);
Prompt3.SetActive(false);
Prompt4.SetActive(true);
}
Answer by Klarzahs · Oct 13, 2020 at 09:30 AM
Hi @Leevai,
it fails because you dont filter the pickup wheter the other objects have been found yet.
If you add e.g. a counter, which indicates the current object, you can easily check that inside each if statement.
Code:
int counter = 0; //0 = no items, 1 = first item, 2 = second,...
...
void OnTriggerEnter(Collider player){
...
if(gameObject.name.equals("Potion blue") && counter == 0){
//disable, enable stuff
counter = 1; // player found first item
}
..
}
Please note that checking strings with "==" is error prone depending on your programming language, so its best-practice to use string.Equals("other string").
Also, did you add the above script to each go to check the OnTriggerEnter method? Then you probably have assigned the prompt1-4 at each script? Why not make them static variables? :)
Hope this helps
Answer by LeeVai · Oct 13, 2020 at 09:30 PM
Thank you for the insight. I had two things I wanted to happen involving 8 game objects: four clues (prompts1-4) and 4 collectible items (one being and invisible trigger for the first clue. As you find an item, the next one becomes active and collectible and you get the clue in the UI.
My original code had a mess of if checks in the OnTriggerEnter method and I was getting "Object of type has been destroyed but you are still tying to access it" error. Ultimately, I created two public methods (Clues and Items) and separated out the if checks. I called them in the OnTriggerEnter method. I also only SetActive(true) the first collectible gameObject in the sequence in the start method. I commented out the rest. I also commented out collectible items that have been destroyed in the if checks of the Items method. I'm sure that description is clear as mud, but it was a simple fix.
Your answer
Follow this Question
Related Questions
Item pick up script not working 2 Answers
Picking up and object and then dropping it again 0 Answers
Pick up Item 1 Answer
How to Pick up Items On Button Press and Add it To Visible Inventory 0 Answers
Different script for each item? 1 Answer