- Home /
Making a camera list
I want my program to be able to randomly select a camera from a list when it first loads a scene. Then, once the user is done with that scene it removes the selected camera from the list and randomly chooses the next one, so on and so forth...
Also, the program loads the same scene over and over until all cameras have been selected.
It does indeed chooses a camera at random at first but sometimes the random camera selector chooses the same one again when I load the level.
Here is the code I have been working on
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[ExecuteInEditMode]
public class TestClass : MonoBehaviour {
Camera viewPort1, viewPort2;
public static List<Camera> viewPorts = new List<Camera>();
public static int randomCameraSelector;
public Rect GUIRectWindow;
void Awake()
{
viewPort1 = GameObject.FindGameObjectWithTag("MainCamera").camera;
viewPort2 = GameObject.FindGameObjectWithTag("IsometricCamera").camera;
viewPorts.Add(viewPort1);
viewPorts.Add(viewPort2);
randomCameraSelector = Random.Range(0, viewPorts.Count - 1);
switch (randomCameraSelector)
{
case 0:
viewPort1.enabled = true;
viewPort2.enabled = false;
viewPorts.Remove(viewPorts[randomCameraSelector]);
break;
case 1:
viewPort1.enabled = false;
viewPort2.enabled = true;
viewPorts.Remove(viewPorts[randomCameraSelector]);
break;
}
}
void Update()
{
if (drawButon(btn_Begin, "Next View Port"))
{
Application.LoadLevel("TestLevel");
}
if(viewPorts.Count < 0)
{
Application.Quit();
}
}
bool drawButon(Rect rect, string buttonName)
{
if (GUI.Button(new Rect(GUIRectWindow.x + rect.x, GUIRectWindow.y + rect.y, rect.width, rect.height), buttonName))
{
return true;
}
return false;
}
}
Can anyone help me figure out why my code chooses the same camera even though I am immediately getting rid of it after it is being selected? Many thanks in advance.
Answer by komodor · Jan 28, 2014 at 02:20 PM
well dunno why it chooses the same camera at first call, but you have logical error there when you remove first camera then the second one becomes first the second call will be always the case 1
@komodor Thank you. Could you show me code snippet on how to fix it then?
one more question ... the goal is to make each action once in random order ?
if (Random.Range(0f, 1f) < 0.5)
{
viewPort1.enabled = true;
viewPort2.enabled = false;
}
else
{
viewPort1.enabled = false;
viewPort2.enabled = true;
}
switch (randomCameraSelector)
{
case 0:
viewPort1.enabled = !viewPort1.enabled;
viewPort2.enabled = !viewPort2.enabled;
viewPorts.Remove(viewPorts[randomCameraSelector]);
break;
case 1:
viewPort1.enabled = !viewPort1.enabled;
viewPort2.enabled = !viewPort2.enabled;
viewPorts.Remove(viewPorts[randomCameraSelector]);
break;
}
i know it's not elegant at all, but basically you need to be ready for both options in the switch
@komodor I figured out the issue. What was happening was that every time I was loading the scene it keeps adding the view ports onto the list. So, no matter if I was removing the index when a view port is selected it was being reloaded again and again.
$$anonymous$$y solution was quite simple.
First, to make sure that it only loads the camera list only once I wrote an if statement that makes sure that it only adds the camera if the list is empty.
Second and last, I created a boolean that checks if its the first run. If its the second time the scene is loaded then load the next camera over.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Printing a GUI selection grid in order 1 Answer
Printing an ordered list 1 Answer
my random task generator not working 1 Answer