Unity C#Object Selection

Asked

Viewed 106 times

0

Well, I’ll get to the point, could give me a force in this script I’m doing, it selects using buttons -1 and 1, until it works more the previous gets bugged

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

public class ObjectSelection : MonoBehaviour {

    public GameObject[] ObjetosSelecionado;


    private int Selecao;


    public void ProximaSelecao(){

        ObjetosSelecionado [Selecao].SetActive (false);

        if (Selecao >= ObjetosSelecionado.Length - 1) {

            Selecao = 0;
            ObjetosSelecionado [Selecao].SetActive (true);

        } else {

            Selecao++;
            ObjetosSelecionado [Selecao].SetActive (true);


        }
    }
  public void AnteriorSelecao(){
        ObjetosSelecionado [Selecao].SetActive (true);

        if (Selecao >= ObjetosSelecionado.Length -1) {

            Selecao = 0;
            ObjetosSelecionado [Selecao].SetActive (false);

        } else {

            Selecao++;
            ObjetosSelecionado [Selecao].SetActive (false);

        }
    }
    // Use this for initialization
    void Start () {

        for (int i = 0; i < ObjetosSelecionado.Length; i++) {

            ObjetosSelecionado [i].SetActive (false);

            Selecao = 0;
            ObjetosSelecionado [0].SetActive (true);

        }

    }

}

1 answer

2


If I understand correctly you want to make it go selecting one by one as a row, the one studied in the code that is very simple to understand.

public void ProximaSelecao()
{
    ObjetosSelecionado [Selecao].SetActive (false);
    Selecao++;
    if (Selecao > ObjetosSelecionado.Length - 1) 
    {
        Selecao = 0;
    }
    ObjetosSelecionado [Selecao].SetActive (true);
}

public void AnteriorSelecao()
{
    ObjetosSelecionado [Selecao].SetActive (false);
    Selecao--;
    if (Selecao < 0) 
    {
        Selecao = ObjetosSelecionado.Length -1;
    }
    ObjetosSelecionado [Selecao].SetActive (true);
}
  • if it’s easy for you, then you can give me a strength, in the next selection I get more the previous one is that stopped me

  • Does the code I put in not work? @Nitecki

  • now was the reverse, the previous works more the next n

  • I think now go, had an equal left that did not let select the last position

  • and I thought it was necessary a for, very obg by the help worked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.