Hi,
I´m new to Scripting and ran into a fundamental problem, i think.
I read and watched allot Tutorials online, and have three DVD´s about Unity and Unity Scripting and still learning!
I´m stuck for days with this and hope you can guide me the right direction.
I setup this simple testing scene, as shown in the image attached.
I´m trying to have two kinds of Buttons to choose from for the User.
First, the “Main”- & “FPS”-Buttons. “Main” mainly as Intrudctionperspective as one starts the Level. And “FPS” as a User controlled Freeroamingperspective.
Second, the “<”- & “>”-Buttons. These should cycle through various set Cameras. (The goal is to have the count of these customizable in the end. But this is the next step. First small.)
I had testes this seperatly. One Project with only a Back&Forth-Buttons which cycle through all cameras, which worked. And another project where every Camera had his own Button, worked although.
But I´m not able to combine these two.
I tried diefferents things, last for example was to exchange every “GameObject”-Type with the “Camera”-Type and vice-a-versa.
My problem is that I learned to use “GameObject” with cameras to de-/activate cameras. But now the Camera-Type confuses me. And I think this maybe the problem : “GameObject.SetActive” VS. “Camera.enabled”
and please, if it´s no problem for you C#. Always switching between C# and JS confuses me and costs more time to me than as it helps. But if it´s the only way you know… I try again to translate it wtihout mistakes^^
…altough a big SORRY for my bad english!
using UnityEngine;
using System.Collections;
public class CameraButtons : MonoBehaviour
{
//Slots for Cameras & help me understand better
public GameObject MainCam;
public GameObject FPSCam;
public GameObject[] perspCameras;
private GameObject Camera1;
private GameObject Camera2;
private GameObject Camera3;
private GameObject Camera4;
//Camera Counter/Index (taken from the web)
private int currentCameraIndex;
private Camera currentCamera;
void Start()
{
perspCameras = new GameObject[] { Camera1, Camera2, Camera3, Camera4 };
// currentCamera = MainCam; //CS0029: Cannot implicitly convert type `UnityEngine.GameObject' to `UnityEngine.Camera'
ChangeView();
}
void Update()
{
}
public void EnableMain() //Main Button, activating MainCamera
{
Debug.Log("Main clicked");
MainCam.SetActive(true);
FPSCam.SetActive(false);
}
public void EnableFPS() //FPS Button, activating FirsPersonControlled-Camera
{
Debug.Log("FPS clicked");
MainCam.SetActive(false);
FPSCam.SetActive(true);
}
public void EnableNext() //Next Button, activates foward cycling through various (here 4) perspective Cameras
{
Debug.Log("Next clicked");
Debug.Log("CamIndex" + currentCameraIndex);
currentCameraIndex++; //copy&paste from internet, i think it counts up till array end, and starts again?!
if (currentCameraIndex > perspCameras.Length - 1)
currentCameraIndex = 0;
ChangeView();
}
public void EnableBack() //Back Button, activates backward cycling through various (here 4) perspective Cameras
{
Debug.Log("Back clicked");
Debug.Log("CamIndex" + currentCameraIndex);
currentCameraIndex--; //modified:copy&paste from internet, i think it counts up till array end, and starts again?!
if (currentCameraIndex > perspCameras.Length - 1)
currentCameraIndex = 0;
if (currentCameraIndex < 0)
currentCameraIndex = perspCameras.Length - 1;
ChangeView();
}
//taken from web: pushing the "s"-Key activates camera cycling/ChangeView
/* void Update()
{
if (Input.GetKeyDown("s"))
{
currentCameraIndex++;
if (currentCameraIndex > perspCameras.Length-1)
currentCameraIndex = 0;
ChangeView();
}
} */
void ChangeView() //tfw: HERE´s a BIG PROBLEM. "SetActive" vs. "enabled" ("GameObject" vs. "camera"?)
{
currentCamera.enabled = false;
currentCamera = perspCameras[currentCameraIndex];//CS0029: Cannot implicitly convert type `UnityEngine.GameObject' to `UnityEngine.Camera'
currentCamera.enabled = true;
}
}





