Question about the camera

Hello, this is a camera switching code, if you do not switch the camera, instead switching like cubes, spheres, how to rewrite the code?

using UnityEngine;

  using System.Collections;

  public class CamControl : MonoBehaviour {

   void Update () {

    if(Input.GetKey("1")){

     Debug.Log("Using Camera One");

     camSwap(1);

    }

    if(Input.GetKey("2")){

     Debug.Log("Using Camera Two");

     camSwap(2);

    }

    if(Input.GetKey("3")){

     Debug.Log("Using Camera Three");

     camSwap(3);

    }

   }

   void camSwap(int currentCam){

    GameObject[] cameras = GameObject.FindGameObjectsWithTag("cam");

    foreach (GameObject cams in cameras){

     Camera theCam = cams.GetComponent<Camera>() as Camera;

     theCam.enabled = false;

    }  

    string oneToUse = "Camera"+currentCam;

    Camera usedCam = GameObject.Find(oneToUse).GetComponent<Camera>() as Camera;

    usedCam.enabled = true;

   }

  }

If I understand you correctly, you’d like to change the code to apply to objects instead of switching between camera views. This script grabs up cameras and uses them based on a tag. In short: you can make use of that tag or a new one to find different objects that are already in your game.

Here’s a more detailed explanation on how to make minimal changes to the script to make it work for you:

In it’s acting function (called “camSwap”), the script uses FindGameObjectsWithTag() to gather the cameras. This makes it easy for you to gather other items instead. Put a tag on your cubes or spheres that you want to be able to select. If you are not using this script for its original purpose, then you can put “cam” as a tag on them, since it’s already set up to look for that, and it’s not a preset tag. If you ARE using this for the cameras as well, make up a different tag to avoid errors, and have this function look for that new tag.

The next part of that function cycles through the objects gathered:

   foreach (GameObject cams in cameras){

     Camera theCam = cams.GetComponent<Camera>() as Camera;

     theCam.enabled = false;

   }  

It’s designed here to turn off the camera component of all the objects one at a time. Since we’re not using cameras, we don’t need to do that, so we can do other things here. For the example, we’ll deactivate the objects we are not selecting. The loop can be simplified to this:

foreach (GameObject cams in cameras){

     cams.active = false;

}  

The final part of the function is designed to activate a camera by name, based on the key that was pressed.

string oneToUse = "Camera"+currentCam;

Camera usedCam = GameObject.Find(oneToUse).GetComponent<Camera>() as Camera;

usedCam.enabled = true;

Again, here, it enables a camera component. Since we’re not dealing with cameras, we’re going to just activate our object. This part of the function finds an object by name, specifically “Camera1”, “Camera2”, or “Camera3”, depending on what key was pressed, which is sent to the function. If you were to name your Cube “Camera1” it would be found when the player pressed 1, for example.

Since it would be confusing to name our cubes and spheres “Camera”, lets just make it look for “Object” instead, and activate it when it’s found.

string oneToUse = "Object"+currentCam; //change the name we're looking for here

GameObject usedObject = GameObject.Find(oneToUse); //look for it by name here

usedObject.active = true; // turn it on here.

So, with those minor changes, you can deactivate everything with your special tag except for the item you select with 1, 2, or 3, as long as those objects are named “Object1”, “Object2”, and “Object3”.

While I made minimal changes to the script to help clarify the differences, I strongly recommend going through and adjusting variable names to be in context with the script’s purpose, and keeping your object names accurate.