Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 AlexGan001 · Jul 30, 2015 at 10:09 AM · arraysvoidsimplifynames

Simplifying code - similar array names

I would like to know if there is a good way to simplify my two routines into one. They both use exactly the same code apart from differently named arrays and gameobjects. It would be good to be able to specify a name to use as the array name such as 'logs' or 'sticks' when the routine is run so that I can have the option to add different values. This would really help with the efficiency of my code. Hope someone can help. Thanks.

Here is my code:

The two routines are called AddToWood & AddToSticks

 public GameObject logsStackPrefab;
     public GameObject[] logsStacks;
     public GameObject sticksStackPrefab;
     public GameObject[] sticksStacks;
 
     // Use this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
         for (int i = 0; i < invSlots.Length; i++) {
             if (invSlots[i].transform.childCount < 1) {
                 slotBools [i] = false;
             } else {
                 slotBools [i] = true;
             }
         }
 
         if (Input.GetKeyDown (KeyCode.Q)) {
             AddToWood (value: 40);
         }
         if (Input.GetKeyDown (KeyCode.R)) {
             AddToSticks (value: 10);
         }
     }
 
     public void AddToWood(int value) {
         bool needNewStack = false;
         int remainder = 0;
 
         for (int s = 0; s < logsStacks.Length; s++) {
             Debug.Log ("s = " + s);
             if (logsStacks[s] == null) {
                 if (s == 0) {
                     needNewStack = true;
                 } else if (s != 0 && logsStacks[s - 1] != null && logsStacks[s - 1].gameObject.GetComponent<InventorySlot>().value == 50) {
                     needNewStack = true;
                 } 
             } else {
                 needNewStack = false;
             }
 
             if (needNewStack) {
                 for (int d = 0; d < invSlots.Length; d++) {
                     if (!slotBools[d]) {
                         GameObject newStack = Instantiate (logsStackPrefab, invSlots[d].transform.position, invSlots[d].transform.rotation) as GameObject;
                         logsStacks[s] = newStack;
                         newStack.transform.SetParent (invSlots[d].transform);
                         newStack.transform.localScale = invSlots[d].transform.localScale;
                         d = invSlots.Length;
                     }
                 }
             }
 
             Debug.Log ("woodStacks[s] = " + logsStacks[s]);
 
             if (logsStacks[s] == null)
                 s = logsStacks.Length;
 
             if (remainder > 0) {
                 Debug.Log ("Recognised that remainder is more than 0");
                 logsStacks[s].gameObject.GetComponent<InventorySlot>().value += remainder;
                 remainder = 0;
                 s = logsStacks.Length;
             } else if (logsStacks[s].gameObject.GetComponent<InventorySlot>().value < 50) {
                 Debug.Log ("Less than 50");
                 logsStacks[s].gameObject.GetComponent<InventorySlot>().value += value;
                 Debug.Log ("Added");
                 if (logsStacks[s].gameObject.GetComponent<InventorySlot>().value > 50) {
                     Debug.Log ("Now Over 50");
                     remainder = logsStacks[s].gameObject.GetComponent<InventorySlot>().value - 50;
                     logsStacks[s].gameObject.GetComponent<InventorySlot>().value = 50;
                     Debug.Log ("Remainder = " + remainder);
                 } else {
                     s = logsStacks.Length;
                 }
             }
         }
     }
 
     public void AddToSticks(int value) {
         bool needNewStack = false;
         int remainder = 0;
         
         for (int s = 0; s < sticksStacks.Length; s++) {
             Debug.Log ("s = " + s);
             if (sticksStacks[s] == null) {
                 if (s == 0) {
                     needNewStack = true;
                 } else if (s != 0 && sticksStacks[s - 1] != null && sticksStacks[s - 1].gameObject.GetComponent<InventorySlot>().value == 50) {
                     needNewStack = true;
                 } 
             } else {
                 needNewStack = false;
             }
             
             if (needNewStack) {
                 for (int d = 0; d < invSlots.Length; d++) {
                     if (!slotBools[d]) {
                         GameObject newStack = Instantiate (sticksStackPrefab, invSlots[d].transform.position, invSlots[d].transform.rotation) as GameObject;
                         sticksStacks[s] = newStack;
                         newStack.transform.SetParent (invSlots[d].transform);
                         newStack.transform.localScale = invSlots[d].transform.localScale;
                         d = invSlots.Length;
                     }
                 }
             }
             
             Debug.Log ("sticksStacks[s] = " + sticksStacks[s]);
             
             if (sticksStacks[s] == null)
                 s = sticksStacks.Length;
             
             if (remainder > 0) {
                 Debug.Log ("Recognised that remainder is more than 0");
                 sticksStacks[s].gameObject.GetComponent<InventorySlot>().value += remainder;
                 remainder = 0;
                 s = sticksStacks.Length;
             } else if (sticksStacks[s].gameObject.GetComponent<InventorySlot>().value < 50) {
                 Debug.Log ("Less than 50");
                 sticksStacks[s].gameObject.GetComponent<InventorySlot>().value += value;
                 Debug.Log ("Added");
                 if (sticksStacks[s].gameObject.GetComponent<InventorySlot>().value > 50) {
                     Debug.Log ("Now Over 50");
                     remainder = sticksStacks[s].gameObject.GetComponent<InventorySlot>().value - 50;
                     sticksStacks[s].gameObject.GetComponent<InventorySlot>().value = 50;
                     Debug.Log ("Remainder = " + remainder);
                 } else {
                     s = sticksStacks.Length;
                 }
             }
         }
     }
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 allenallenallen · Jul 30, 2015 at 11:17 AM

You can combine AddToWood and AddToSticks into one AddToThing.

 void AddToThing(int value, GameObject stackPrefab, GameObject[] stacks){
    // Same code here; just change the variable names to the 2 extra variables I added above.
 }

And you can call this function using:

 AddToThing(40, logsStackPrefab, logsStacks); // For your logs.
 AddToThing(10, sticksStackPrefab, sticksStacks); // For your sticks.

Now both items are just using one single function.

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

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

22 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

Related Questions

Variable not accesible 2 Answers

How to convert a 'string' to the name of a 'void' function? 1 Answer

How to instantiate a random object from an array? 3 Answers

IndexOutOfRangeException for initial spawn of prefabs from an array 1 Answer

Implementing a 2D array as a field on a ScriptableObject? 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