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 zcoldrick · Mar 08, 2014 at 10:27 PM · prefabarraysetactive

setactive doesn't appear to work on instanciated prefab gameobject array

I wrote some code for toggling through cameras:

It works but isn't elegant and doesn't allow for adding more cameras easily.

     using UnityEngine;
     using System.Collections;
     using System.Collections.Generic;
     
     public class ChooseCamera : MonoBehaviour {
     
         
         public GameObject Standard_Camera_Prefab;
         public GameObject FlyCam;
         private GameObject Standard_Camera;    
         private GameObject Fly_Camera;
     
         private int CameraIndex =0;
     
         // Use this for initialization
         void Awake () {
     
                     //Instantiate All Cameras
     
             Standard_Camera = Instantiate (Standard_Camera_Prefab) as GameObject;
             Standard_Camera.SetActive (true);
     
             Fly_Camera = Instantiate (FlyCam) as GameObject;
             Fly_Camera.SetActive (false);
     
                     }
             }
         
     
         void Update () {
         
     
             if (Input.GetButtonUp ("ToggleCameras")) {
     
                 if (Standard_Camera.activeSelf){
                     Standard_Camera.SetActive(false); CameraIndex=1;
                 }
                 if (Fly_Camera.activeSelf){
                     Fly_Camera.SetActive(false); CameraIndex=0;
                 }
     
     
     
                     }
             // set new camera active
     
             if (CameraIndex == 0)
                             Standard_Camera.SetActive (true);
             if (CameraIndex == 1)
                             Fly_Camera.SetActive (true);
     
         }
     }


So I decided to re-write it as a prefab array of gameobjects containing cameras so I can add as many as I like by expanding the array.

However no matter what I do, the numbers iterate properly which I can observer in the inspector BUT the .SetActive(bool) does nothing!

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ChooseCameraFromArray : MonoBehaviour {
     
     public GameObject[] CameraArrayOfPrefabs;
     public int cameraIndex;
 
 
     void Awake () {
         
     //Instantiate All Cameras into the array
     foreach (GameObject item in CameraArrayOfPrefabs) 
         {
         GameObject NewCamera = (GameObject)Instantiate(item) as GameObject;
         }
 
     foreach (GameObject item in CameraArrayOfPrefabs) 
         {
         item.gameObject.SetActive(false);
         }
 
         CameraArrayOfPrefabs [0].gameObject.SetActive(true);
         cameraIndex = 0 ;
     }
 
     void Update () {
 
         if (Input.GetButtonUp ("ToggleCameras")) 
         {
 
             CameraArrayOfPrefabs[cameraIndex].SetActive(false);
 
             if (CameraArrayOfPrefabs.Length-1 != cameraIndex) 
                 {
                 cameraIndex += 1;
                 }
                 else
                 {
                 cameraIndex = 0;
                 }
             //set camera index to active
 
             CameraArrayOfPrefabs[cameraIndex].SetActive(true);
         }
     }
 }

Is there something I'm missing with prefab gameobjects in arrays ?

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

2 Replies

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

Answer by robertbu · Mar 08, 2014 at 10:51 PM

Instantiate() creates a copy or clone of an object. First question: is the CameraArrayOfPrefabs really an array of prefab items? Assuming so, on line 16, you create copies of each camera in the array, but you are not capturing those copies. That is you have the 'originals' or 'blueprints' for your cameras in the CameraArrayOfPrefabs, but they are not the same as the copies you make. You are iterating through the array of prefabs setting the active state of the prefabs, but that does not in any way impact the copies you made through Instantiate(). You need to capture each camera you create in a new array or generic list. It is that new array or list you iterate through when you want to set the active state of those objects.

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 zcoldrick · Mar 08, 2014 at 11:37 PM

ok solved it similarly to how you suggested, not sure if having two arrays is the most effective way to deal with it but perhaps it is.

 using System.Collections.Generic;
 
 public class ChooseCameraFromArray : MonoBehaviour
 {
     
         public PersistentScript thePersistentScript;
         public GameObject[] CameraArrayOfPrefabs;
         private GameObject[] myCameraArray;
         public int cameraIndex;
 
         // Use this for initialization
         void Awake ()
         {
         
                 //Instantiate All Cameras into the array
                 myCameraArray = new GameObject[CameraArrayOfPrefabs.Length];
                 for (int i = 0; i < CameraArrayOfPrefabs.Length; i++) {
                         GameObject myGameObject = new GameObject ();
                         myGameObject = Instantiate (CameraArrayOfPrefabs [i]) as GameObject;
                         myCameraArray [i] = myGameObject;
 
 
                 }
 
                 foreach (GameObject item in myCameraArray) {
                         item.SetActive (false);
                 }
 
 
                         myCameraArray [0].SetActive (true);
                         cameraIndex = 0;
 
 
         }
     
 
         void Update ()
         {
 
                 if (Input.GetButtonUp ("ToggleCameras")) {
 
                         myCameraArray [cameraIndex].SetActive (false);
                         print ("Camera index set to" + cameraIndex);
 
 
                         if (myCameraArray.Length - 1 != cameraIndex) {
                                 cameraIndex += 1;
                         } else {
                                         cameraIndex = 0;
                         }
                         //set camera index to active
 
                         myCameraArray [cameraIndex].SetActive (true);
 
 
 
         
                 }
         }
 }
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

20 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

Related Questions

Using an object pool with Javascript. 1 Answer

Destroying Instantiated Prefab 1 Answer

Resizeable gameObject array. 1 Answer

Constantly search for enemys with certain tag, if close enough set nav agent destination to it 1 Answer

Array simple problem 1 Answer


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