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
1
Question by Panundrum · Feb 11, 2021 at 03:49 AM · arrayarrayselementelementscase

Using class arrays for multiple Variables per element also Naming Elements

I want to populate an array so that each element has 2 fields in the inspector, a string and a bool. I have 5 objects with the tag "TouchZone" in the scene. I want to populate the array using GameObject.FindGameObjectsWithTag("TouchZone") I have the following:

     [System.Serializable]
     public class Data
     {
         public string objectName;
         public bool objectBool;
     }
     public Data[] dataArray;
 

I know I need a for loop, I just don't understand how to add to the array or how to write the for loop properly to add the data to the array properly.

         int x = GameObject.FindGameObjectsWithTag("TouchZone").Length;
         for (int i = 0; i < x; i++)//Get the list length of Scenes
         {
             //What do I add here?
         }

I figure I need to add it to the for loop above but I dont know the proper code to add the data into the case array

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

2 Replies

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

Answer by Bunny83 · Feb 11, 2021 at 04:40 AM

You should rethink your approach. First of all an array can not change its size. The size of an array need to be specified when the array is created. Second it wasn't really clear what exact data you want to store in that string and boolean value. If it's the name of the objects you found through the FindGameObjectsWithTag method it would make more sense to store the actual references to those objects in your class. Specifically something like that:

 [System.Serializable]
 public class Data
 {
     public GameObject obj;
     public bool objectBool;
 }
 public Data[] dataArray;
 
 // [ ... ]
 var objs = GameObject.FindGameObjectsWithTag("TouchZone");
 
 // create new Data array with the same number of elements as we have in the objs array.
 dataArray = new Data[objs.Length];
 
 for (int i = 0; i < objs.Length; i++)
 {
     // create new Data object
     var tmp = new Data();
     
     // set the values you want.
     tmp.obj = objs[i];
     tmp.objectBool = false;
     
     // store the Data object in our dataArray
     dataArray[i] = tmp;
 }


If, for some reason you really want to store a copy of the names of the objects, instead of storing the reference to the object in the class you just store the name (or whatever you want from those object). So if the Data object looks like your original one, you would do something like

 tmp.objectName = objs[i].Name;
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
1

Answer by Panundrum · Feb 11, 2021 at 05:03 AM

OH MY god you are my HERO! I have been pounding my head on the desk for HOURs and hours! It finally displays the info I want: alt text

My full code for just this example that is working thanks to @Bunny83 is this (for those that were interested):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class boolArray : MonoBehaviour
 {
 
     //public bool[] touchZonesToggle;
 
 
     [System.Serializable]
     public class Data
     {
         public GameObject obj;
         public bool objectBool;
     }
     public Data[] dataArray;
 
 
 
     void Start()
     {
         // [ ... ]
         var objs = GameObject.FindGameObjectsWithTag("TouchZone");
 
         // create new Data array with the same number of elements as we have in the objs array.
         dataArray = new Data[objs.Length];
 
         for (int i = 0; i < objs.Length; i++)
             {
             // create new Data object
             var tmp = new Data();
 
             // set the values you want.
             tmp.obj = objs[i];
             tmp.objectBool = false;
 
             // store the Data object in our dataArray
             dataArray[i] = tmp;
             }
 
         }
 
     void Update()
     {
 
     }
 
 }
 
 


workingclassarray.jpg (43.1 kB)
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 Panundrum · Feb 11, 2021 at 05:27 PM 0
Share

I added the public string name in the class and the name in the for loop so that it would set the fields in the elements to the name of the object. (Keep in $$anonymous$$d I also changed the var to the proper variable type for my own knowledge) I wanted to change the name of the elements too when I first started this, so I am happy to add it here now.

     //Class for the layout (Blueprint) of a later array
     [System.Serializable]
     public class Data
     {
         //the name string is to set the Element name in the inspector
         //Hide the name [Field] in the inspector because it shows the name in the element
         [HideInInspector]
         public string name;
         public GameObject obj;
         public bool objectBool;
     }
     //The Public array for the Class:
     public Data[] dataArray;
 
 
 
     void Start()
     {
         // Do a search for gameobjects with the matching tag store that in a var
         GameObject[] objs = GameObject.FindGameObjectsWithTag("TouchZone");
 
         // create new Data array with the same number of elements as we have in the objs array.
         dataArray = new Data[objs.Length];
 
         for (int i = 0; i < objs.Length; i++)
             {
             // create new Temporary Data object
             Data tmp = new Data();
 
             // set the values you want.
             tmp.obj = objs[i];
             tmp.objectBool = false;
             //This sets the name of the element
             tmp.name = objs[i].name;
 
             // store the Data object in our dataArray
             dataArray[i] = tmp;
             }
 
         }
 

Hope this helps someone else too. This is the outcome:

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

128 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

Related Questions

Setting arrays equal to each other issue 2 Answers

Using variables inside a GameObject[] array 1 Answer

Multidimensional arrays - editing one element within one of the arrays 1 Answer

Multidimensional array trouble 2 Answers

Prefabs instantiated from an array are keeping their public int value 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