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 /
  • Help Room /
avatar image
0
Question by Stonepunsh-Studio · Jan 17, 2018 at 07:44 PM · unity 5array

I cant reference Objects in an array. Int array works

Hello, i made an array and tried to make the array full of public game objects (Panel1,Panel2,...). But i cant include the Panel in the array. I can call the object in every other function but not in the array. An int array (ints) works perectly. I think a verry simple misstake. Thank you for your help

 public GameObject Panel1;public GameObject Panel2;public GameObject Panel3;public GameObject Panel4;
     public GameObject[] Panels = new GameObject[]{Panel1,Panel2,Panel3,Panel4};
     public int[] ints = new int[]{ 1, 2, 3, 4, 5 };    
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
1
Best Answer

Answer by davedevN · Jan 17, 2018 at 07:59 PM

Looks like you can't do "new GameObject[]{Panel1,Panel2,Panel3,Panel4};" in an initializer. Do it in a method instead.,Don't know for sure, but you probably can't do that in an initializer. So move "Panels = new GameObject[]{Panel1,Panel2,Panel3,Panel4};" to a Start or other script. In fact, if you are using Visual Studio, roll over "Panel1" with the red squiggle under, it will tell you can't do that and why.

Comment
Add comment · Show 3 · 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 Stonepunsh-Studio · Jan 18, 2018 at 07:36 PM 0
Share

Sadly that didnt work. But i found the solution myself. You new to make the the Panels static. Now you can use them in the array but you cant assign them to an object. So you need a few GetPanel vars not static. First you set now the Panels to GetPanels and everything works fine. But thank you for your help.

     public GameObject GetPanel1; public GameObject GetPanel2; public GameObject GetPanel3; public GameObject GetPanel4;
         public static GameObject Panel1;public static GameObject Panel2;public static GameObject Panel3;public static GameObject Panel4;
         public GameObject[] PublicPanels;
      void Start () {
             Panel1 = GetPanel1; Panel2 = GetPanel2; Panel3 = GetPanel3; Panel4 = GetPanel4;
             GameObject[] Panels= new GameObject[] { Panel1, Panel2, Panel3, Panel4 };}
 
avatar image davedevN · Jan 19, 2018 at 11:15 AM 0
Share

Your solution is unorthodox unless you have some specific purpose other that to "just make it work". Why are you using the statics.?Buckslice had a $$anonymous$$or error in redeclaring Panels. This should work

 public GameObject Panel1;
 public GameObject Panel2;
 public GameObject Panel3;
 public GameObject Panel4;
 public GameObject[] Panels;
 void Start () {
  Panels = new GameObject[] { Panel1, Panel2, Panel3, Panel4 };
     for(int i = 0; i < Panels.Length; i++)
     {
         Panels[i].SetActive(false);
     }
 }
avatar image Stonepunsh-Studio davedevN · Jan 19, 2018 at 03:56 PM 0
Share

I now found another solution wich works perfectly without any static or somethig. Works similar to yours. But thank you for your help. Though you get the correct for your help xD

avatar image
0

Answer by Buckslice · Jan 17, 2018 at 08:01 PM

Try just doing:

 public GameObject[] Panels;

and then assign your panels in the inspector.
Or you could call this part of your code in the start function instead

 void Start(){
     public GameObject[] Panels = new GameObject[]{Panel1,Panel2,Panel3,Panel4};
 }
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 Stonepunsh-Studio · Jan 18, 2018 at 07:37 PM 0
Share

Sadly that didnt work. But i found the solution myself. You new to make the the Panels static. Now you can use them in the array but you cant assign them to an object. So you need a few GetPanel vars not static. First you set now the Panels to GetPanels and everything works fine. But thank you for your help.

     public GameObject GetPanel1; public GameObject GetPanel2; public GameObject GetPanel3; public GameObject GetPanel4;
         public static GameObject Panel1;public static GameObject Panel2;public static GameObject Panel3;public static GameObject Panel4;
         public GameObject[] PublicPanels;
      void Start () {
             Panel1 = GetPanel1; Panel2 = GetPanel2; Panel3 = GetPanel3; Panel4 = GetPanel4;
             GameObject[] Panels= new GameObject[] { Panel1, Panel2, Panel3, Panel4 };}
 

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

159 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

Related Questions

Building tycoon game, need help storing variables into an array. 1 Answer

is it possible toggle bool at editor by script? 1 Answer

How to make a 2D array of buttons? 2 Answers

All GameObjects list to a GameObject? 0 Answers

Array size resets to 0 in inspector. 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