Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Collect object in a correct order

Discussion in 'Scripting' started by Riario93, Dec 10, 2019.

  1. Riario93

    Riario93

    Joined:
    Apr 6, 2019
    Posts:
    21
    Hey gentlemen,

    I have a little code to write but I am not sure how to do it.

    Basically, I have 3 objects to pickup and I need to pick them up in order, let’s simply say from 1 to 3.

    If I pick the correct object in the order nothing will happen just keep playing.
    Instead if I pick Up the third object rather then the first the player will die.

    do you have any universal code idea for this?

    thanks
     
  2. Vectorbox

    Vectorbox

    Joined:
    Jan 27, 2014
    Posts:
    232
    You could add your 'target' objects to a list in numerical order [target1, target2, target3]. When you attempt to pick-up a target object do the following...

    1. If the target object list index equals 0, remove it from the list and continue
    2. If the target object list index does not equal 0 the player is eliminated

    Example Code

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class TargetHandler : MonoBehaviour {
    5.     // Public References
    6.     public List<Transform> targetCache; // Add Target Objects In Inspector
    7.  
    8.     // --
    9.     void OnTriggerEnter(Collider col) {
    10.         // Identify Collision Object
    11.         if (targetCache.Contains(col.transform)) {
    12.             if (targetCache.IndexOf(col.transform) == 0) {
    13.                 // Debug Message
    14.                 print("Pickup Collected");
    15.                 // Remove Target Object From List
    16.                 targetCache.RemoveAt(0);
    17.                 // >> Your Pickup Code Here <<
    18.             }
    19.             else {
    20.                 // Debug Message
    21.                 print("Player Eliminated");
    22.                 // Your Eliminate Player Code Here <<
    23.             }
    24.         }
    25.     }
    26. }
     
    Last edited: Dec 10, 2019
    Riario93 likes this.
  3. Riario93

    Riario93

    Joined:
    Apr 6, 2019
    Posts:
    21
    That’s awesome. But it looks like it refers only for the element 0 of the list. Doesn’t it mean that once I pick up the first object the function will still check the element 0 while the second object is in the index 1?

    Another thing I’m not sure about are those condition inside the if. I have a player with tag “Player” that I guess I should use it as a collision identifier is it right ? How should I write it ?
     
  4. Vectorbox

    Vectorbox

    Joined:
    Jan 27, 2014
    Posts:
    232
    Hi

    As the collision removes the first target object from the list, the next available target object will be assigned index 0. This will continue until the list is empty.

    Keep in mind that the list index refers to the position of the object in the list as opposed to the name of the game object.

    For example, the script would be attached to the 'Player' object. When a collision is detected (in this case a trigger) the script checks if the col.transform exists in the 'targetCache' list. No object tags are required.

    How you configure the script will depend on your specific project.
     
    Last edited: Dec 10, 2019
  5. Riario93

    Riario93

    Joined:
    Apr 6, 2019
    Posts:
    21
    problem solved. thanks a lot
     
    Vectorbox likes this.