0
\$\begingroup\$

Here is the code for my forward selection:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CustomizeModels : MonoBehaviour
{   public GameObject[] outfits;
    private int currentOutfit;

    void Start()
    {
        currentOutfit = 0;
    }

    void Update()
    {
        for (int i = 0; i < outfits.Length; i++)
        {
            if (i == currentOutfit)
            {
                outfits[i].SetActive(true);
            }
            else
            {
                outfits[i].SetActive(false);
            }
        }
    }

    public void SwitchOutfits()
    {
        if (currentOutfit == outfits.Length - 1)
        {
            currentOutfit = 1;
        }
        else
        {
            currentOutfit++;
        }
    }

}

How do I create a backward selection?

\$\endgroup\$
2
  • \$\begingroup\$ You seem to have the basic idea: check if you're at the end of the list and if so, start over at the other end of the list. Otherwise, change your index by 1 in the direction you want to cycle. Where specifically did you run into trouble making this work for iteration in the other direction? \$\endgroup\$
    – DMGregory
    Commented Oct 13, 2023 at 16:31
  • \$\begingroup\$ But you might not want to call your code in Update in a loop like that, only when you actually need it. While this small piece might not make a problem, add enough of them and they will \$\endgroup\$
    – Zibelas
    Commented Oct 13, 2023 at 17:33

1 Answer 1

1
\$\begingroup\$

Your SwitchOutfits() method skips the first and last element of the array. The following code does not.

Here is the commented refactored code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CustomizeModels : MonoBehaviour
{   
    public GameObject[] outfits;
    private int currentOutfit;
    private int previousOutfit;

    void Start()
    {
        currentOutfit = 0;
        previousOutfit = 0;
        outfits[currentOutfit].SetActive(true); // set initial active
        // active should be false on all `outfit`s during creation
    }

    void Update()
    {
        if(currentOutfit == previousOutfit)
            return; // shortcut exit if there is no change

        outfits[previousOutfit].SetActive(false); 
        outfits[currentOutfit].SetActive(true);
        previousOutfit = currentOutfit;
    }

    public void SwitchOutfits(int direction = 1) // use -1 for reverse
    {
        currentOutfit += direction;
        if (currentOutfit < 0 || currentOutfit >= outfits.Length)
            currentOutfit = (currentOutfit + outfits.Length) % outfits.Length;
        // The addition handles the negative case: outfits.Length + (-1) = outfits.Length - 1
        // The mod then shifts the value between 0 and outfits.Length - 1
    }
}

If you prefer the use of ifs instead of the mod %:


    public void SwitchOutfits(int direction = 1) // use -1 for reverse
    {
        currentOutfit += direction;

        if (currentOutfit == outfits.Length)
        {
            currentOutfit = 0;
        }
        else
        {
            if (currentOutfit == -1)
            {
                currentOutfit = outfits.Length - 1;
            }
        }
    }
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .