Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mmangual_83 · Jan 28, 2014 at 01:27 PM · c#cameralistrandom

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mmangual_83 · Jan 28, 2014 at 02:23 PM 0
Share

@komodor Thank you. Could you show me code snippet on how to fix it then?

avatar image komodor · Jan 28, 2014 at 03:10 PM 0
Share

one more question ... the goal is to make each action once in random order ?

avatar image mmangual_83 · Jan 28, 2014 at 03:29 PM 0
Share

@komodor correct basically its like this:

alt text

cameralist.png (24.8 kB)
avatar image komodor · Jan 28, 2014 at 04:15 PM 0
Share
         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

avatar image mmangual_83 · Jan 28, 2014 at 05:34 PM 1
Share

@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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Disabling a child script within the parent. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges