Activate one object and disable others

1

I'm doing a car shop system in the Unity engine, and I need a method to activate a contour type on the selected car. However, I need the contour of the other cars to automatically turn off. The script I already have is this:

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

public class CarShop : MonoBehaviour {

    public string carroSelected;
    public Text velMaxima;
    public Text Acele;
    public Text cambio;
    public Text Motor;
    public GameObject capContorno;
    public GameObject copContorno;
    public GameObject viaContorno;
    public GameObject nviaContorno;

    // Use this for initialization
    void Start () {
        velMaxima.text = "0 km/H";
        Acele.text = "0s";
        cambio.text = "0 Marchas";
        Motor.text = "NULL";
    }

    // Update is called once per frame
    void Update () {

    }

    public void capitalSel () {
        carroSelected = "Capital";
        velMaxima.text = "110 km/h";
        Acele.text = "21.90s";
        cambio.text = "4 Marchas";
        Motor.text = "Boxxer";
        capContorno.SetActive(true);
        copContorno.SetActive(false);
        viaContorno.SetActive(false);
        nviaContorno.SetActive(false);
    }

    public void copaSel () {
        carroSelected = "Copa";
        velMaxima.text = "170 km/h";
        Acele.text = "10.3s";
        cambio.text = "5 Marchas";
        Motor.text = "AP (Alta Performance)";
        capContorno.SetActive(true);
        copContorno.SetActive(false);
        viaContorno.SetActive(false);
        nviaContorno.SetActive(false);
    }

    public void viagemSel () {
        carroSelected = "Viagem";
        velMaxima.text = "170 km/h";
        Acele.text = "11.5s";
        cambio.text = "5 Marchas";
        Motor.text = "AP (Alta Performance)";
        capContorno.SetActive(true);
        copContorno.SetActive(false);
        viaContorno.SetActive(false);
        nviaContorno.SetActive(false);
    }

    public void nViagemSel () {
        carroSelected = "Novo Viagem";
        velMaxima.text = "190 km/h";
        Acele.text = "10s";
        cambio.text = "5 Marchas";
        Motor.text = "Power EA";
        capContorno.SetActive(true);
        copContorno.SetActive(false);
        viaContorno.SetActive(false);
        nviaContorno.SetActive(false);
    }   
}
    
asked by anonymous 14.02.2018 / 03:20

1 answer

0

What you can do is create a list of GameObjects and within it put your outlines, but first you must delete these multiple variables outline of your attributes, your code start should look like this:

public class CarShop : MonoBehaviour {
  public string carroSelected;
  public Text velMaxima;
  public Text Acele;
  public Text cambio;
  public Text Motor;
  public List<GameObject> Contornos; 
  //restante do código
}

To add your contours to the list is simple in the editor of unity there will be a new field called Outlines just drag all the GameObject that equals the outline of the car inwards, as this new attribute is a list whenever an element is added he expects a close one, but does not need to put, just put the ones you want.

Now enter the part of deactivating the contours of this list, first create a method to deactivate, in this case I called Disable, this method should receive a GameObject to search it in the list we created earlier, and after receiving that GameObject it will identify if it exists in the list and disable all others other than that same GameObject and activate whatever GameObject, the code will look like this:

//metodo que ira desativar e ligar todos os contornos
public void Desativar (GameObject Contorno)
{
   //desativa todos os itens da lista
   for(int i=0; i < Contornos.Length; i++)
   {
    Contornos[i].SetActive(false);
   }
   //metodo que vasculha a lista e ativa o certo
   foreach (GameObject GOBJ in Contornos)
   {
    //se algum elemento da lista for igual ao que procura
     if(GOBJ.GetInstanceID() == Contorno.GetInstanceID() )
      GOBJ.SetActive(true);//ativa elemento igual o que esta procurando
   }
}
    
07.03.2018 / 16:17