Script for activation of a Camera, A Button and a Light from a List

Hi to all,
I’m a begginer in coding and I found my first wall in this scripts.
The idea is that when I puss a GameObject Buton it uses Loops to get the IDs (int numbers) for each respective Camera, Button and Light and activates only that selectet and deactivates the rest in the scene but idk how to make it properly work.

Thanks to everyone that is willing to give any davice.

public class BotonesCRT : MonoBehaviour, IInteractuable
{
    [Header("Datos")]
    public int ID;
    public int Indice;
    public Vector3[] Posiciones;
    [Header("Movimiento")]
    private bool DeboMoverme;
    private bool MovimientoContinuo;
    private float Velocidad = 0.00001f;
    [Header("Audios")]
    [SerializeField] private AudioSource SonidoBotonRCT;
    [SerializeField] private AudioClip ClickBotonRCT;
    void Update()
    {
        Movimiento();
    }
    public void AlInteractuar()
    {
        GestorDeCamaras.Instancia.ActivarIDs(ID);
    }
    public void ActivarBotones(bool pulsado)
    {
        if (pulsado)
        {
            DeboMoverme = true;
            SonidoBotonRCT.volume = PlayerPrefs.GetFloat("Volumen_Master", 0.5f);
            SonidoBotonRCT.PlayOneShot(ClickBotonRCT);
        }
        else
        {
            DeboMoverme = false;
            SonidoBotonRCT.volume = PlayerPrefs.GetFloat("Volumen_Master", 0.5f);
            SonidoBotonRCT.PlayOneShot(ClickBotonRCT);
        }
    }
    private void Movimiento()
    {
        if (!DeboMoverme)
        {
            return;
        }
        transform.localPosition = Vector3.MoveTowards(transform.localPosition, Posiciones[Indice], Velocidad * Time.deltaTime);
        ComprobarDistancia();
    }
    public void ComprobarDistancia()
    {
        if (Vector3.Distance(transform.localPosition, Posiciones[Indice]) <= 0.01f)
        {
            transform.localPosition = Posiciones[Indice];
            HeLlegadoADestino();
        }
    }
    public void HeLlegadoADestino()
    {
        DeboMoverme = MovimientoContinuo;
        Indice++;
        if (Indice >= Posiciones.Length)
        {
            Indice = 0;
        }
    }
}
public class GestorDeCamaras : MonoBehaviour
{
    [Header("Lists")]
    public static GestorDeCamaras Instancia;

    public List<BotonesCRT> Botones;
    public List<IDCamaras> Camaras;
    public List<IDLinternas> Luces;

    private void Awake()
    {
        Instancia = this;
    }
    public void Start()
    {
        for (int i = 0; i < Botones.Count; i++)
        {
            Botones[i].ID = i;
        }
        for (int i = 0; i < Camaras.Count; i++)
        {
            Camaras[i].ID = i;
        }
        for (int i = 0; i < Luces.Count; i++)
        {
            Luces[i].ID = i;
        }
    }
    public void ActivarIDs(int ID)
    {
        for (int i = 0; i < Botones.Count; i++)
        {
            Botones[i].ActivarBotones(Botones[i].ID == ID);
        }
        for (int i = 0; i < Camaras.Count; i++)
        {
            Camaras[i].ActivarCamaras(Camaras[i].ID == ID);
        }
        for (int i = 0; i < Luces.Count; i++)
        {
            Luces[i].ActivarLinternas(Luces[i].ID == ID);
        }
    }
}
public class IDCamaras : MonoBehaviour
{
    public int ID;

    public void ActivarCamaras(bool opcion)
    {
        if (opcion)
        {

        }
        else
        {

        }
    }
}
public class IDLinternas : MonoBehaviour
{
    public int ID;

    public void ActivarLinternas(bool opcion)
    {
        if (opcion)
        {
            
        }
        else
        {
            
        }
    }
}

Sounds like you wrote a bug… and that means… time to start debugging!

The first place I would start is wherever you found this code, as a ton of it is obviously incomplete and would do nothing, such as 100% of the “ActivarCamera()” method.

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.

Two steps to tutorials and / or example code:

  1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
  2. stop and understand each step to understand what is going on.

If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.

Imphenzia: How Did I Learn To Make Games:

No, I wrote it myself but I don’t know how to wrote the lines that activates only the camera with the same ID as the Button pressed on ActivarCamaras().

Simplest: You connect each button you want to the camera you want it to activate.

Far more complex:

If you want a generic messaging system where an ID can be changed or typed in and then looked up in some kind of camera repository, that is a LOT more moving parts:

  • camera repository that tracks all cameras and their IDs (such as a Dictionary)
  • hooks from button to pass an ID to this lookup and find the camera
  • code to do whatever you want to the cameras.

Send the ID as well as the true?

Camaras[i].ActivarCamaras(Camaras[i].ID == ID, ID);

public void ActivarCamaras(bool opcion, int ID);

You might not need the bool, actually.

Thanks for all the help, but at the end I seetle up with this. (I don’t know why it didn’t think this before.)

´´´´
public class IDCamaras : MonoBehaviour
{
public int ID;
[Header(“Esta Camara”)]
public GameObject CamaraActual;
[Header(“Las Otras”)]
public GameObject Camara1;
public GameObject Camara2;
public GameObject Camara3;
public GameObject Camara4;
public void ActivarCamaras(bool opcion)
{
if (opcion)
{
CamaraActual.SetActive(true);
Camara1.SetActive(false);
Camara2.SetActive(false);
Camara3.SetActive(false);
Camara4.SetActive(false);
}
else
{
CamaraActual.SetActive(false);
}
}
}
´´´´