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 /
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.

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

Dynamic Array Generation via Inspector 1 Answer

how subtract total value element of array ?? 1 Answer

How to see what element is the current one 2 Answers

How do I access a variables created in a function with another function? 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