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 unity_vxVxYx9XYfh1IQ · Mar 05, 2020 at 09:59 AM · cameraarray

Switching between 3 cameras doesn't work

Hello, I have a problem with my code. I don't get why my cameraList.Lenght is equal to 2 and not 3 as I have a size of 3 in the inspector and 3 cameras in my gameobject[] cameraList.

If someone can explain what i'm doing wrong that'd be awesome!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SwitchCamera : MonoBehaviour
 {
     // Start is called before the first frame update
 
     public GameObject[] cameraList;
     int currentIndex;
 
     void Start()
     {
         Debug.Log("cameralenght = " + cameraList.Length);
         currentIndex = 0;
         cameraList[currentIndex].SetActive(true);
 
     }
 
     // Update is called once per frame
     public void Button_Click()             
     {
         Debug.Log("cameralenght2 = " + cameraList.Length);
 
         DesactivateAll();
 
         if (currentIndex < cameraList.Length-1)
         {
             currentIndex++;
         }
         else 
         { 
             currentIndex = 0; 
         }
 
         cameraList[currentIndex].SetActive(true);
 
         Debug.Log("Index = " + currentIndex);
       
 
 
     }
 
     void DesactivateAll ()
     {
         foreach (GameObject cam in cameraList)
         {
             cam.SetActive(false);
         }
     }
 
 }
 

Thanks you for your time

Comment
Add comment · Show 2
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 tormentoarmagedoom · Mar 05, 2020 at 10:51 AM 1
Share

Good day. This is a "standard response"

For this kind of problems, you need to learn to find your problem by your own. As you can imagine, we can not try to read all scripts people posts, understand the logic and process, what all variables means, when or how you use them, and find where is the "problem". You are the only one who can do it...

You need to debug the code while running, and check the states of all variables at the moment you see the problem, and I'm sure you will detect what is not how it was suposed to be.

If don't know how, look for some tutorials on how to debug code while running.

Bye & good Luck!


If cameraList.Lenght is equal to 2 is because its 2.

Explore why is happening. Debug your code to see its lenght all frames, before each function..

avatar image unity_vxVxYx9XYfh1IQ tormentoarmagedoom · Mar 05, 2020 at 07:45 PM 0
Share

I tried to debug the code and ask for some friends but I didn't get why the problem is happening. I know that people can't help everyone but I just can't get why this is occuring. Sorry ;c

1 Reply

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

Answer by ATLGAN · Mar 05, 2020 at 11:10 AM

I tried this code. it's working well. Also i edited your code. I added current camera object variable for optimization. using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class SwitchCamera : MonoBehaviour
 {
     [SerializeField] GameObject[] m_CameraList;
     GameObject m_CurrentCamera;
 
     int m_CurrentIndex;
 
     void Start()
     {
         //DesactivateAll();
         Debug.Log("cameralenght = " + m_CameraList.Length);
         m_CurrentIndex = 0;
         m_CurrentCamera = m_CameraList[m_CurrentIndex];
         m_CurrentCamera.SetActive(true);
         //m_CameraList[m_CurrentIndex].SetActive(true);
 
     }
     public void Button_Click()
     {
         Debug.Log("cameralenght2 = " + m_CameraList.Length);
 
         //DesactivateAll();
 
         if(m_CurrentIndex < m_CameraList.Length - 1)
         {
             m_CurrentIndex++;
         }
         else
         {
             m_CurrentIndex = 0;
         }
 
         m_CurrentCamera.SetActive(false);
         m_CurrentCamera = m_CameraList[m_CurrentIndex];
         m_CurrentCamera.SetActive(true);
         //m_CameraList[m_CurrentIndex].SetActive(true);
 
         Debug.Log("Index = " + m_CurrentIndex);
 
 
 
     }
     void DesactivateAll()
     {
         foreach(GameObject cam in m_CameraList)
         {
             cam.SetActive(false);
         }
     }
 
 }
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 unity_vxVxYx9XYfh1IQ · Mar 05, 2020 at 07:54 PM 0
Share

That seems to work fine, I don't get what changed between your code and $$anonymous$$e but I'll take it. Thank you !!!

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

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

Camera movement - index out of range 1 Answer

Toggle Control between multiple turrets 0 Answers

Array for multiple cameras? 2 Answers

Accessing next Camera from array 1 Answer

How do I change which camera is active based on text dialogue? 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