• 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 ShadyProductions · Dec 30, 2015 at 02:23 PM · arrayvariablesarray of gameobjectselementelements

Using variables inside a GameObject[] array

Hello fellow programmers,

I'm trying to use a class inside my GameObject[] array so I can access them in the inspector however, I'm not entirely sure how to do that, right now what I got is something like the following: alt text

However I would like to achieve something like this: alt text

This is the base of my code right now:

     [System.Serializable]
     public class tiles
     {
         public GameObject[] Plants;
         public GameObject[] Trees;
         public GameObject[] Rocks;
     }

     [System.Serializable]
     public class tileFunctions
     {
         public bool isInteractive; //can u click on it and do things with it?
         public bool isUsed; //is it on cooldown after use?
         public int maxReceivings; //if gathered how many items max
     }

I would like to use the tileFunctions inside Plants, Trees and Rocks. So that I could use them as follows for example: groundTiles[0].Plants[0].isUsed

Thanks! Regards, Shady.

13938-capture.jpg (30.0 kB)
13937-capture2.jpg (20.5 kB)
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

1 Reply

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

Answer by jmonasterio · Jan 03, 2016 at 04:04 AM

Assuming you have Plant, Tree, and Rock classes (that each inherit from monoBehaviour), you might get more mileage from keeping arrays of the specific types instead of gameObjects.

 [System.Serializable]
  public class tiles
  {
      public Plant[] Plants;
      public Tree[] Trees;
      public Rock[] Rocks;
  }

Then in your Plant object, for example, you can have a tileFunctions object like:

 public class Plant : MonoBehaviour
 {
    public tileFunctions tile_funcs;
 }

and you can access this in code as:

   tiles.Plants[2].tile_funcs.isUsed = true; //Example

Although that may give you what you're asking for, it's not really the unity way AFAIK. Really, your TileFunctions should be a MonoBehaviour. And then you add your TileFunction component/script to your Plant.

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 ShadyProductions · Jan 03, 2016 at 12:42 PM 0
Share

Thanks I'll figure it out!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

35 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

Related Questions

Using class arrays for multiple Variables per element also Naming Elements 2 Answers

how can i check if there is a blanc spot in my list 3 Answers

Array element to variable 1 Answer

why if loops doesnot work? 1 Answer

comparing variables of gameobjects in an array 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