C# Uber noob question: Replacing a number with an int variable

Although I’ve already managed to do relatively pretty fancy stuff in procedual programming some simple concepts still elude me.

How do I replace the variable “CameraNumber” with the number “1” under “OnTriggerEnter”?

using UnityEngine;
using System.Collections;

public class CameraCollider : MonoBehaviour {
    public Camera Camera1;
    private int CameraNumber;

    // Use this for initialization
    void Start () {
        CameraNumber = gameObject.name[13];
    }
 
    // Update is called once per frame
    void OnTriggerEnter () {
         
            Camera1.enabled = true;
    }
}
CameraNumber = 1;

What exactly do you mean by replace? Are you trying to set the variable equal to 1 or are you going for something else?

I already set the variable CameraNumber = 1

CameraNumber = gameObject.name[13];

I’m just not sure how do I set it instead of the number “1” I have written here:

Camera1.enabled=true;

Several variations I tried didn’t work like
Camera(CameraNumber).enabled=true;

or

Camera.CameraNumber.enabled=true;

or

Camera[CameraNumber].enabled=true;

etc…

Not possible. Variable names are not dynamic. You cannot build a variable’s name through code, and then access that variable.

There are Many Good Reasons for this. If you post what you’re trying to do, we could hint at a better way of doing it.

Baste, it’s not dynamic, it’s equal to 1. It’s really not what my question is about. Just imagine that the variable CameraNumber is = 1;

In fact, let’s do it like this:

CaermaNumber = 1;

Great, now we can be sure that CameraNumber = 1
So…how do I use it here?

CameraCameraNumber.enabled=true;

?

To clarify an issue for you:

CameraNumber = gameObject.name[13];

May not return the number you expect. This will return the ASCII value of the 13th character in the name of this gameobject. So the character if the 13th character in the gameobject name is a “1” then 49 is stored in CameraNumber.

To get back to your main question. You want to access an array of all cameras in scene.I’m not aware of a good way to retrieve all cameras in the scene (GetAllCameras only gets active cameras) so I setup this array manually using the inspector by dragging all cameras into the allCameras entry for this script. You can likely come up with a better solution if needed.

public Camera[] allCameras;

Then you’ll be able to do something like

allCameras[CameraNumber].enabled=true;

Still impossible.

You’re trying to use the text “Camera” and the variable value of 1, and combine them together into the variable name Camera1. You cannot do this in regular, C# programming, as there’s no real link between the sourcecode variable names and the runtime values of those variables.

What you’re trying to do could be done in an interpreted programming language. You could do it with the help of dictionaries - by mapping string-names to camera variables, and you could do it through the dirty, dirty hack of reflection. All of these solutions are far more advanced than simply doing this in some sane way, and are also really, really, really slow.

What you’re trying to do is common. The solution is arrays. Implicit in your example is that you have multiple cameras.

Instead of labeling them
Camera Camera1;
Camera Camera2;

Why not make an array of Camera’s?

Camera[] Cam;

Cam[1] = //here's a reference storage for your camera one;
Cam[2] = //here's a reference storage for your camera two;

Now, you can do what you want, which is take an integer value and use it to refer to this or that Camera. In other words:

Cam[CameraNumber];
1 Like

I tried your method, skillbased, and got an error of “unexpected symbol” and “invalid rank”

    int CameraNumber;
    Camera[] MyCameras;
    MyCameras[0] = Camera0;
    MyCameras[1] = Camera1;

If you just want to find a camera named “Camera1” or “Camera2” why don’t you just use the GameObject.Find method.

public class CameraCollider : MonoBehaviour {
   public Camera CamToToggle;
   private int CameraNumber;
  
   // Use this for initialization
   void Start ()
     {
     CameraNumber = 1;
     }
  
   // Update is called once per frame
   void OnTriggerEnter () {
    
     CamToToggle = GameObject.Find("Camera"+CameraNumber).GetComponent<Camera>();    
     CamToToggle.enabled = false;
   }
}

The array has to be initialized with the number of slots required to store all of your camera’s. So if you have 2 Cameras, you initialize it like so:

Camera[] MyCameras = new Camera[2];