Home > other >  How to skip the element in unity
How to skip the element in unity

Time:11-29

I have this function

    public void RemoveInvalidEntries()
    {
         characters = characters.Where(c => c).ToArray();
        
    }

    public void NextOption()
    {

        characters = characters.Where(c => c).ToArray();

        characters[selectedCharacter].SetActive(false);
        selectedCharacter = (selectedCharacter   1) % characters.Length;
        characters[selectedCharacter].SetActive(true);
        

        
    }

that helps to remove elements from the list and skip to the next character.

But I don't want to remove the element I want to skip to the next available. As I need to know the id of the selected element, for example: Element 0,Element 1 or Element 2

I managed to get the selected character Id, but as elements are removed at this moment from the list selected character id is always 0;

enter image description here

I need to maintain elements in the list as in this picture that I can get information which character was selected.

enter image description here

How can I skip to the next available element?

If I am doing everything wrong please tell me as I am learning.

derHugo helped me last time to remove elements.

CodePudding user response:

Well do you want to remove them or only skip them? If you remove them why care about the index? Of course after removing the first to elements your last remaining element i moved from index 2 to index 0

otherwise you could just jump over them using e.g.

do
{
    selectedCharacter  = (selectedCharacter   1) % characters.Length;
}
while(!characters[selectedCharacter]) 

have in mind though that this is extremely dangerous since if there is simply no valid item remaining at all then this freezes your entire app!

So you will want to check first if there is at least one valid element at all via

public void NextOption()
{
    if(!characters.Any(c => c))
    {
        Debug.LogError("No valid objects available!");
        return;
    }

    characters[selectedCharacter].SetActive(false);

    do
    {
        // go one index forward with wrap around
        selectedCharacter  = (selectedCharacter   1) % characters.Length;
    }
    // if reached an invalid object repeat the "do" block
    while(!characters[selectedCharacter]);
    
    characters[selectedCharacter].SetActive(true);
}

public void BackOption()
{
    if(!characters.Any(c => c))
    {
        Debug.LogError("No valid objects available!");
        return;
    }

    characters[selectedCharacter].SetActive(false);

    do
    {
        // go one index backward with wrap around
        selectedCharacter--;
        if(selectedCharacter < 0)
        {
            selectedCharacter  = characters.Length;
        }
    }
    // if reached an invalid object repeat the "do" block
    while(!characters[selectedCharacter]);

    characters[selectedCharacter].SetActive(true);
}

where characters.Any(c => c) basically is a shorthand / equals doing something like

private bool Any()
{
    foreach(var c in characters)
    {
        if(c) return true;
    }

    return false;
}
  • Related