Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 imM4TT · Dec 18, 2017 at 10:49 PM · camerasceneview

Enable / Disable cameras on the scene

Hello guys, following this topic https://answers.unity.com/questions/1443599/unable-to-change-camera.html?childToView=1444185#comment-1444185 , my issue is now concrete.


I have a script with 2 public cameras, and I have assigned them on the inspector, more precisly I placed the prefabs of these camera in the inspector
So when I said " cam1.enabled = false; " on my script, the prefab of this camera will turn off, but the cam1 on the scene will not be affected.
Is there any way to disable / enable cameras on the scene, please ?

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

5 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by WorldEater · Dec 19, 2017 at 03:37 PM

The prefab and the instance of the prefab are two separate objects, you need a reference to the camera in the scene to modify it. An easy way to get this reference is to store it when you spawn the camera.

 GameObject cameraObject = Instantiate (cameraPrefab, spawnPos, spawnRot) as GameObject;

Then use the camera object to edit whatever you want. Alternatively drag the camera in the scene into your script.

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 imM4TT · Dec 19, 2017 at 03:47 PM 0
Share

Ok thank you. Ill try like this because I cant put the camera in the scene into their variable, I just can put their prefab but it's useless.

avatar image WorldEater imM4TT · Dec 19, 2017 at 04:30 PM 0
Share

No problem, if you bump into any issues feel free to ask.

avatar image
0

Answer by programer717 · Dec 19, 2017 at 04:35 PM

@imM4TT I would not use prefabs but what I do is set the cameras where I want them set them all on the same display and use the number keys to change here is the script that I use most often. :public Camera camera1; public Camera camera2;

 // Use this for initialization
 void Start () {

     camera1.enabled = true;
     camera2.enabled = false;
 }

 // Update is called once per frame
 void Update () {
     if (Input.GetKey ("1")){
         camera2.enabled = false;camera1.enabled = true;
 }

     if (Input.GetKey ("2")) {
         camera1.enabled = false;camera2.enabled = true;
     }

 
     }
 }

sorry if this is not what you meant in your question but one thing you have to make sure for this script is all the cameras are on the same display.

Comment
Add comment · Show 1 · 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 imM4TT · Dec 19, 2017 at 05:28 PM 0
Share

Yep It sounds nice but I cant assign the cameras which are in the scene on their public variable ...

avatar image
0

Answer by imM4TT · Dec 19, 2017 at 04:57 PM

When I try your method I got an error like that :
Cannot convert type UnityEngine.Camera' to UnityEngine.GameObject' via a built-in conversion
GameObject cameraObject = Instantiate(Camera, transform.position, transform.rotation) as GameObject;

Comment
Add comment · Show 4 · 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 imM4TT · Dec 19, 2017 at 07:20 PM 0
Share

Any idea ?

avatar image imM4TT · Dec 20, 2017 at 04:02 PM 0
Share

Im still trying to resolve this issue :D

avatar image Hellium · Dec 20, 2017 at 04:04 PM 1
Share
  GameObject cameraObject = Instantiate(Camera.gameObject, transform.position, transform.rotation) as GameObject;
avatar image imM4TT Hellium · Dec 20, 2017 at 06:14 PM 0
Share

thank you very much Hellium

avatar image
0

Answer by Adil_Alhilali · Jun 18, 2020 at 05:18 PM

  void Off_All_Cameras()
     {
         object[] Cams = GameObject.FindObjectsOfType(typeof(Camera));
         foreach (Camera C in Cams)
         {
            if(C.name != PlayerCamera.name)
             {
                 C.enabled = false;
             }
         }
     }
Comment
Add comment · 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
0

Answer by jmsachin · Sep 29, 2021 at 11:18 AM

I have the same problem but neither of these comments help me and I don't know what to do now , btw I am using Unity Networking AKA uNet so I have to use prefabs to spawn in the player and I cannot insert my camera into my array until it is a separate prefab and as I said earlier those lines of script given by @WorldEater and @Hellium don't help me in any way, tell me if you guys have any fix for this.

Comment
Add comment · 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

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

117 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 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 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

Weird shader behavior , when displacing vertices. 1 Answer

Scene-like camera control in Game 1 Answer

Inconsistency between game view, main camera object, and camera preview 1 Answer

Using Command Buffers with the Scene View Camera 0 Answers

Scene View Camera Depth Texture? 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