• 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 Inryhk · Jan 04, 2021 at 06:08 PM · arrays

Connecting/syncing two arrays ?

 public class Mine : MonoBehaviour
 {
     [SerializeField] int health = 35;
     [SerializeField] ItemObject[] yield;
     [SerializeField] int[] amount;
 
     public void Mining(int strength)
     {
         if (health-strength > 0)
         {
             health -= strength;
         }
         else
         {
             Destroy(gameObject);
         }
     }
 
     public void OnDestroy()
     {
         for (int i = 0; i < yield.Length; i++)
         {
             Pickup pickup = Instantiate(PrefabsRef.prefabs.Pickup, transform.position, Quaternion.identity).GetComponent<Pickup>();
             pickup.ThrowAway(yield[i], amount[i]);
         }
     }
 }
 

Simple issue really, I have one class that handles all my pickups, so whenever I want to drop multiple stacks of items I just instantiate it multiple times using for loop.
As you see above my current implementation is not perfect and prone to errors as the two arrays are not synced. I have to set up items and the amounts seperately and I would prefer to set amount per each item entry.
Is there an easy way of doing it better or connecting those two arrays at least in the inspector ?

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 unity_ek98vnTRplGj8Q · Jan 04, 2021 at 06:29 PM

Rather than trying to sync 2 arrays usually its better to compile all the data into 1 array. Something like this:

 private struct ItemStack
 {
     ItemObject item;
     int amount;
 }
 
 ItemStack[] itemYeild;
 
 public void OnDestroy(){
     for(int i=0; i<itemYeild.Length; i++){
         pickup.ThrowAway(itemYeild[i].item, itemYeild[i].amount);
     }
 }
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 Inryhk · Jan 04, 2021 at 06:58 PM 0
Share

Thanks for quick answer! I already tried creating a seperate system.serializable class that would hold this info together which kinda works.

 [System.Serializable]
 public class Stack
 {
     public ItemObject item;
     public int amount;
 }

But struct makes so much more sense. Thanks for the help.

avatar image Eno-Khaon Inryhk · Jan 04, 2021 at 08:28 PM 0
Share

Just a heads up about this:

Queue and Stack are common (albeit less commonly seen in C# than lower-level languages) Collection types used in program$$anonymous$$g.

While it might not interfere with your project, it's a safer bet to not replace a Stack in case you decide you want to use one later.

avatar image Inryhk Eno-Khaon · Jan 06, 2021 at 01:13 PM 0
Share

Oh? thanks. So it would be cleaner to change the name of the struct so that it doesn't interfere with the collection type. Will do.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

114 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

Related Questions

How do I indicate that an Array is an Array? 1 Answer

MutliDimensionnal Array public size 2 Answers

Array not updating in inspector 0 Answers

How can i put the text from an array to a generated button prefab? 1 Answer

Easier way to resize group of buttons to the same size 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