Activate an object and disable others

Asked

Viewed 667 times

2

I’m making a car shop system in the Unity engine, and I need a method to activate a type of contour in the selected car. However, I need the contour of the other cars to deactivate automatically. 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);
    }   
}

1 answer

0

What you can do is create a Gameobjects list and within it put its outlines, but first you should delete these multiple variables outline of its 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 outlines the list is simple in the Unity editor there will be a new field called Outlines is just dragging all Gameobject that equals the outline of the car inside, as this new attribute is a list whenever an element is added it expects a next one, but does not need to put, just put the ones you want.

Now enter the part to disable the outlines of that list, first create a method to disable, in which case I called Disable, that method should receive a Gameobject to perform its search in the list we created previously, and after receiving this Gameobject it will identify if it exists in the list and disable all others that are not this same Gameobject and activate whatever Gameobject is, 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
   }
}

Browser other questions tagged

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