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

Snowboard Game Checkpoint Route? (Like Steep)

Discussion in 'Scripting' started by AtmoGen, May 13, 2020.

  1. AtmoGen

    AtmoGen

    Joined:
    Mar 25, 2020
    Posts:
    2
    Hi everyone! I was hoping you could help me with some code involving a series of checkpoints.

    So I'm trying to make a snowboard track where you have to go through checkpoints. There will only be 2 checkpoints visible everytime. Once you go through the first checkpoint of the 2, this one will disappear and another checkpoint will show up further ahead in the map at a set location.

    I tried doing this by using an array, destroying the checkpoint when going through and then enabeling the next one, altough this doesn't seem to work because it can't find the next checpoint in the array.

    Can anyone please help me with this?

    This is my code I'm currently using. (C#)

    public class CheckpointSystem : MonoBehaviour
    {
    public GameObject[] _checkpointObject;
    int _amountOfCheckpointsShown = 0;
    int _checkpointAmount;

    private void Start()
    {

    }
    void Update()
    {
    CheckpointCaller();
    }

    private void CheckpointCaller()
    {
    for(_checkpointAmount = _checkpointObject.Length-1; _checkpointAmount < _checkpointObject.Length; _checkpointAmount++)
    {
    _checkpointObject[_checkpointAmount].SetActive(_checkpointAmount == _amountOfCheckpointsShown);
    }
    }

    private void OnTriggerEnter(Collider other)
    {
    Destroy(other.gameObject);
    _amountOfCheckpointsShown--;
    }
    }

    Thanks in Advance!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    First, use code tags when posting code, it makes your code legible.

    So on this line:
    Code (csharp):
    1. for(_checkpointAmount = _checkpointObject.Length-1; _checkpointAmount < _checkpointObject.Length; _checkpointAmount++)
    I'll point out that this for loop as written is absolutely pointless: it only executes for a single index. This single index is also always the last object in the array - no other objects will ever have this code run on it. I think the complexity of this for loop is the reason for the issue.

    I'd say replace this code with a simple, straightforward for loop that goes over all items in the array. If necessary, do a null check on the item to make sure it hasn't been destroyed. Also, use a local variable for the loop index and not a private member. The for loop has a popular standard format because it works 99.9% of the time, and it absolutely will work here.
    Code (csharp):
    1. for (int i=0; i < _checkpointObject.Length; i++) {
    2. if (_checkpointObject[i] != null) {
    3. _checkpointObject[i].SetActive(i == _amountOfCheckpointsShown);
    4. }
    5. }
    That said, I'm not sure I'm following why you're destroying objects in the array? Just disable them.

    Also I'm pretty sure you want _amountOfCheckpointsShown++ and not -- at the end.
     
    AtmoGen likes this.
  3. AtmoGen

    AtmoGen

    Joined:
    Mar 25, 2020
    Posts:
    2
    Hi, thank you for your quick respond!
    I've been tinkering around with what you said and eventually I could make it work, thanks!
    This was also my first time posting something so thank you for pointing out that I should use code tags.