Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 vittu1994 · May 31, 2016 at 05:50 PM · setactivefor-loopcamerasfalsetrue

SetActive - setactive(true) for one object, setactive(false) to the rest

Hey i have a GameObject array full of cameras. These are linked to different enums which are operated with a switch state.

 switch (state)
         {
             case State.Player1:
                 SwitchCameras();
                 cameras[1].SetActive(true);
                 break;
             case State.Player2:
                 SwitchCameras();
                 cameras[2].SetActive(true);
                 break;
             case State.none:
                 SwitchCameras();
                 cameras[3].SetActive(true);
                 break;
         }

What i want to do is when one of the camera = true, then the other cameras = false. And this i want to control with the SwitchCameras function:

 void SwitchCameras(){
         for (int i = 0; i < cameras.Length; i++){
            //stuff
         }
     }

The foor loop will check through all cameras. The question here is how it can see a gameobject that is active, and then know to disable the others. And then when a before false camera becomes true the others become false again.

Thanks

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
1
Best Answer

Answer by Soos621 · May 31, 2016 at 06:31 PM

What you want to do is when one of the cameras is activated you will want to pass a reference of that camera component then check the array for all cameras that are not the same so something like

 private Camera[] allCams;
 
 void SwitchCamera (Camera activatedCam){
     for(int i = 0; i < allCams.length; i++){
         if(!allCams[i].Equals(activatedCam)){
             allCams[i].enabled = false;
         } else {
             allCams[i].enabled = true;
         }
     }
 }

Comment
Add comment · Show 2 · 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 vittu1994 · May 31, 2016 at 07:13 PM 0
Share

Hey thanks a lot it worked :) i edited it abit so that it would use gameobject array and not camera array. Because cameras uses audio listener and its best to deactivate cameras so there is always only one listener that is active.

Here it is:

 void SwitchCameras(GameObject activatedCamera){
         for (int i = 0; i < cameras.Length; i++){
             if(!cameras[i].Equals(activatedCamera)){
                 cameras[i].SetActive(false);
             }
             else{
                 cameras[i].SetActive(true);
             }    
         }
     }
avatar image Soos621 · May 31, 2016 at 07:15 PM 0
Share

No problem if you ever get a chance look up lists ins$$anonymous$$d of arrays they are a bit easier to deal witg

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

44 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Set Bool in Inspector to true or False 2 Answers

Problems with save 1 Answer

What's wrong code if-else with tag script ? 0 Answers

Array SetActive C# Unity 1 Answer

I can't figure out how to enable / disable a trigger. 2 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