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 Hamesh81 · Feb 19, 2014 at 02:15 PM · arrayvariablemultipledataaccess

How can I access the variables of arrays with multiple data types?

I've created my own type of array with multiple data types in C#, and then defined it as a public variable:

 [System.Serializable]
 public class MyType{
     
     private GameObject[] allObjectsTagged;
     public string tag;
     public Material objectMaterial;
     
 }

 public class ArrayTest : MonoBehaviour {
 
     public MyType[] objects;

 }

Now I can't work out how I'm supposed to access the variables in my new array. For example, in the start function I would like to use FindGameObjectsWithTag to populate the array "allobjectsTagged" with all the objects tagged by the string variable "tag" above. This way whenever a new element in the "objects" array is assigned, all the objects tagged in this element will be stored in a variable and ready for me to access. But since these are declared in a different class I cannot seam to access them in the start function. Any suggestion how I could do this?

Comment
Add comment · Show 4
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 Nick4 · Feb 19, 2014 at 03:22 PM 0
Share
 void Start()
 {
    allObjectsTagged = new GameObject[ArrayTest.$$anonymous$$yType.Length];
 
    for(int i = 0; i < allObjectsTagged.Length; i++)
    {
       allObjectsTagged[i] = ArrayTest.$$anonymous$$yType[i];
    }
 }

Is this what you're looking for? You can also use lists. Lists would fit better since they're resizable.

avatar image Hamesh81 · Feb 19, 2014 at 03:46 PM 0
Share

When I do that I get a:

error CS0103: The name `allObjectsTagged' does not exist in the current context

This is my main problem, I can't seam to be able to access the variables from the custom class. I've tried the following but it doesn't work either:

 objects[].allObjectsTagged = GameObject.FindGameObjectsWithTag(tag);
avatar image Nick4 · Feb 19, 2014 at 04:52 PM 1
Share

Ah, I forgot to tell you that you should also define objects static to be able to reach from another script directly.

 public static $$anonymous$$yType[] objects;

Let me know if that works for you.

avatar image Hamesh81 · Feb 20, 2014 at 12:00 AM 0
Share

Thanks for your help Nick4 but I can't actually set the variable as static because I need multiple of these in each scene.

1 Reply

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

Answer by whydoidoit · Feb 19, 2014 at 03:51 PM

Something like this would do it:

    void Start() {
       foreach(var o in objects) {
            o.allObjectsTagged = GameObject.FindObjectsWithTag(o.tag);
       }
    }
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 Hamesh81 · Feb 19, 2014 at 04:22 PM 0
Share

Hmm I'm still getting the same error though, I can't work out why:

 error CS0103: The name `allObjectsTagged' does not exist in the current context
avatar image Hamesh81 · Feb 19, 2014 at 11:57 PM 0
Share

Ah I worked it out, I forget that I needed to make the allObjectsTagged variable a public variable since otherwise it cannot be accessed. After that your solution works. Thanks a lot

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

Store multiple types of data in an Array? 4 Answers

Getting variables of objects in an array, then adding them together 1 Answer

Both way expandable array for voxels 1 Answer

Playing multiple sounds on same key input ("e") 1 Answer

Access variables from another script 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