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*.transform.childCount < 1) {*
_ slotBools = false;_
* } else {*
_ slotBools = 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 == null) {*
~~ if (s == 0) {~~
~~ needNewStack = true;~~
~~ } else if (s != 0 && logsStacks[s - 1] != null && logsStacks[s - 1].gameObject.GetComponent().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 = newStack;
~~ newStack.transform.SetParent (invSlots[d].transform);~~
~~ newStack.transform.localScale = invSlots[d].transform.localScale;~~
~~ d = invSlots.Length;~~
~~ }~~
~~ }~~
~~ }~~
Debug.Log ("woodStacks = " + logsStacks~~);~~
if (logsStacks == null)
~~ s = logsStacks.Length;~~
~~ if (remainder > 0) {~~
~~ Debug.Log (“Recognised that remainder is more than 0”);~~
logsStacks~~.gameObject.GetComponent().value += remainder;~~
~~ remainder = 0;~~
~~ s = logsStacks.Length;~~
} else if (logsStacks~~.gameObject.GetComponent().value < 50) {~~
~~ Debug.Log (“Less than 50”);~~
logsStacks~~.gameObject.GetComponent().value += value;~~
~~ Debug.Log (“Added”);~~
if (logsStacks~~.gameObject.GetComponent().value > 50) {~~
~~ Debug.Log (“Now Over 50”);~~
remainder = logsStacks~~.gameObject.GetComponent().value - 50;~~
logsStacks~~.gameObject.GetComponent().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 == null) {
~~ if (s == 0) {~~
~~ needNewStack = true;~~
~~ } else if (s != 0 && sticksStacks[s - 1] != null && sticksStacks[s - 1].gameObject.GetComponent().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 = newStack;
~~ newStack.transform.SetParent (invSlots[d].transform);~~
~~ newStack.transform.localScale = invSlots[d].transform.localScale;~~
~~ d = invSlots.Length;~~
~~ }~~
~~ }~~
~~ }~~
Debug.Log ("sticksStacks = " + sticksStacks~~);~~
if (sticksStacks == null)
~~ s = sticksStacks.Length;~~
~~ if (remainder > 0) {~~
~~ Debug.Log (“Recognised that remainder is more than 0”);~~
sticksStacks~~.gameObject.GetComponent().value += remainder;~~
~~ remainder = 0;~~
~~ s = sticksStacks.Length;~~
} else if (sticksStacks~~.gameObject.GetComponent().value < 50) {~~
~~ Debug.Log (“Less than 50”);~~
sticksStacks~~.gameObject.GetComponent().value += value;~~
~~ Debug.Log (“Added”);~~
if (sticksStacks~~.gameObject.GetComponent().value > 50) {~~
~~ Debug.Log (“Now Over 50”);~~
remainder = sticksStacks~~.gameObject.GetComponent().value - 50;~~
sticksStacks~~.gameObject.GetComponent().value = 50;~~
~~ Debug.Log ("Remainder = " + remainder);~~
~~ } else {~~
~~ s = sticksStacks.Length;~~
~~ }~~
~~ }~~
~~ }~~
~~ }~~