How's it going everyone. I'm looking to forward cycle through a set of object, deactivate the current object and active the next. Also be able to do this from any point of the iteration (not having to start at zero each time), - Start at the last known active object. Here's the End Goal: Have a set of objects[6] Deactivate Last Active Object Activate Next Object Forward looping capabilities (Change to next) Remember last Active Object capabilities If curActive is greater than total length Deactivate Last Object in array reset to beginning of the array Set first in array to active. Continue This loops. I used this approach not sure what I'm failing to realize or implement here: Start: iGO = CW; gameObjects [] go's; int CW; int iGO; method(){ int cW; cW = iGO; for (i =cW; i < go's.length; i++ ){ CW++; iGO++; go's.SetActive(false); go's[i + 1].SetActive(true); } methodSetLastOff(); } methodSetLastOff(){ int Cw; Cw = go's[].Length; go's[Cw -1].SetActive(false); go's[0].SetActive(true); } Any help with the solution for this will be greatly appreciated. Thank you and as always stay productive. --Grand Rio
First thing: Use [code ]code tags[/code], they make things easier to read, and including an "i" in brackets won't mangle the remainder of your post as it's done here. Second thing: Give your variable real names, especially in pseudocode. It makes your code much easier to understand. You're not being charged per letter, and you have access to autocomplete. As for your actual problem - you seem to be trying to use as few words as you can to explain what you want, and it's not good for clarity. I really can only take a guess at what you're trying to accomplish. What I think you want is: an array of objects and an index, and at any arbitrary point, you want to be able to change that index, and deactivate the previous object and activate the new one. Is that about right? If so, you don't need to loop at all. One method would be to make your index a property that handles the deactivation/activation when it gets changed: Code (csharp): public int activeIndex { get { return _activeIndex; } set { if (myArray != null && myArray.Length > 0) { //keep us in the range of the array, looping around as needed while (value < 0) { value += myArray.Length; } while (value > myArray.Length) { value -= myArray.Length; } myArray[_activeIndex].SetActive(false); myArray[value].SetActive(true); _activeIndex = value; } else { _activeIndex = value; } } } private int _activeIndex = 0; public GameObject[] myArray; Then, at any time, you can set activeIndex to any number (or call ++ or -- on it, too) and it'll handle everything.
True indeed apologies for such brief and rushed posting @StarManta. It's a weapon script with a weapon change method that use one button to change weapons only forward cycle and if the cycle goes beyond the total length, reset to the first in the array. Also remember what object was active last so when the instance occurs again set the last used weapon active So you're saying the active return method will do just fine??? This is the script; Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class WeaponController : MonoBehaviour { public enum weaponType{ unArmed, HandGun, SubMachine, Heavy, Assualt, Special } public weaponType weaponT; public enum weaponTypeBurst{ unArmed_Burst, HandGun_Burst, SubMachine_Burst, Heavy_Burst, Assualt_Burst, Special_Burst } public weaponTypeBurst weaponTburst; public int curWeapon; public int iCH; public GameObject[] curWeapon_Go; public CustomEvents CustomEventsScript; public GameObject Text_Ammo_; public Text Text_Ammo_text; public GameObject Text_Clips_; public Text Text_Clips_text; public GameObject Player_; public Player PlayerScript; public int ReadyAmmo = 30; // show this on GUI public int ammoDifference; // dont show this on GUI public int ammo = 30; // show this on GUI // monetize ammo type public int clips = 1; // show this on GUI // monetize clip type public int magCapacity = 7; // show this on GUI // monetize clip extensions, change magCapacity per weapon public int ReloadAmmo_Needed; public void start(){ ammoTrack (); } public void ammoTrack(){ if(curWeapon != 0 ){ clips = ammo / magCapacity; ammoDifference = ammo - (clips * magCapacity); ReloadAmmo_Needed = magCapacity - ReadyAmmo; } } public void SetWeapon(){ switch(curWeapon){ case 0: // unarmmed weaponT = weaponType.unArmed; if(weaponT == weaponType.unArmed){ weaponTburst = weaponTypeBurst.unArmed_Burst; magCapacity = 0; clips = 0; } break; case 1: // handgun weaponT = weaponType.HandGun; if(weaponT == weaponType.HandGun){ weaponTburst = weaponTypeBurst.HandGun_Burst; magCapacity = 14; } break; case 2: // subMachine weaponT = weaponType.SubMachine; if(weaponT == weaponType.SubMachine){ weaponTburst = weaponTypeBurst.SubMachine_Burst; magCapacity = 30; } break; case 3: // heavy weaponT = weaponType.Heavy; if(weaponT == weaponType.Heavy){ weaponTburst = weaponTypeBurst.Heavy_Burst; magCapacity = 14; } break; case 4: // assualt weaponT = weaponType.Assualt; if(weaponT == weaponType.Assualt){ weaponTburst = weaponTypeBurst.Assualt_Burst; magCapacity = 30; } break; case 5: // special weaponT = weaponType.Special; if(weaponT == weaponType.Special){ weaponTburst = weaponTypeBurst.Special_Burst; magCapacity = 1; } break; default : weaponT = weaponType.unArmed; if(weaponT == weaponType.unArmed){ weaponTburst = weaponTypeBurst.unArmed_Burst; magCapacity = 0; clips = 0; } break; } } public void Reload(){ if(ReadyAmmo == magCapacity){ return; } if(ReadyAmmo == 0 && clips == 0 && ammo == 0 && ammoDifference == 0){ return; } if (ReadyAmmo > 0 && ReadyAmmo < magCapacity) { CustomEventsScript.PlayerEventTrigger (6); // play reload sound if (ammo < magCapacity) { ammo -= ammoDifference; ReadyAmmo = ammoDifference; ammo = 0; } else { ammo -= ReloadAmmo_Needed; ReadyAmmo = ReadyAmmo+ ReloadAmmo_Needed; } } if (ReadyAmmo == 0) { CustomEventsScript.PlayerEventTrigger (6); // play reload sound if (ammo < magCapacity) { ammo -= ammoDifference; ReadyAmmo = ammoDifference; ammo = 0; } else { ammo -= ReloadAmmo_Needed; ReadyAmmo = ReadyAmmo+ ReloadAmmo_Needed; } //} } } public void ChangeWeapon(){ int cW; cW = curWeapon; for(int i = cW; i < curWeapon_Go.Length; i++){ curWeapon++; i = iCH; //curWeapon_Go[i + 1].SetActive(true); curWeapon_Go[i].SetActive(false); SetWeapon (); } ChangeWeapon_LastOff (); SetWeapon (); } public void ChangeWeapon_LastOff(){ int LastGo; LastGo = curWeapon_Go.Length; curWeapon_Go[iCH].SetActive(false); //curWeapon_Go [i + LastGo - 1].SetActive (false); curWeapon_Go [0].SetActive (true); curWeapon = 0; } public void ChangeWeapon_Set(int CurWeapon){ switch(CurWeapon){ case 0: // unarmmed curWeapon_Go[0].SetActive(true); curWeapon_Go[1].SetActive(false); curWeapon_Go[2].SetActive(false); curWeapon_Go[3].SetActive(false); curWeapon_Go[4].SetActive(false); curWeapon_Go[5].SetActive(false); break; case 1: // handgun curWeapon_Go[0].SetActive(false); curWeapon_Go[1].SetActive(true); curWeapon_Go[2].SetActive(false); curWeapon_Go[3].SetActive(false); curWeapon_Go[4].SetActive(false); curWeapon_Go[5].SetActive(false); break; case 2: // subMachine curWeapon_Go[0].SetActive(false); curWeapon_Go[1].SetActive(false); curWeapon_Go[2].SetActive(true); curWeapon_Go[3].SetActive(false); curWeapon_Go[4].SetActive(false); curWeapon_Go[5].SetActive(false); break; case 3: // heavy curWeapon_Go[0].SetActive(false); curWeapon_Go[1].SetActive(false); curWeapon_Go[2].SetActive(false); curWeapon_Go[3].SetActive(true); curWeapon_Go[4].SetActive(false); curWeapon_Go[5].SetActive(false); break; case 4: // Assualt curWeapon_Go[0].SetActive(false); curWeapon_Go[1].SetActive(false); curWeapon_Go[2].SetActive(false); curWeapon_Go[3].SetActive(false); curWeapon_Go[4].SetActive(true); curWeapon_Go[5].SetActive(false); break; case 5: // Special curWeapon_Go[0].SetActive(false); curWeapon_Go[1].SetActive(false); curWeapon_Go[2].SetActive(false); curWeapon_Go[3].SetActive(false); curWeapon_Go[4].SetActive(false); curWeapon_Go[5].SetActive(true); break; default : curWeapon_Go[0].SetActive(true); curWeapon_Go[1].SetActive(false); curWeapon_Go[2].SetActive(false); curWeapon_Go[3].SetActive(false); curWeapon_Go[4].SetActive(false); curWeapon_Go[5].SetActive(false); break; } } public void BuyAmmo(){ if(PlayerScript.CharMoneyOT > 100){ PlayerScript.CharMoneyOT -= 100; ammo += 30; CustomEventsScript.ShopEventTrigger (1); // enough, purchase sound } } public void SubtractAmmo(){ if(ReadyAmmo > 0){ ReadyAmmo--; } } public void SetAmmo(){ switch(curWeapon){ case 0: ReadyAmmo = 0; break; case 1: if (ammo > magCapacity) { ReadyAmmo = magCapacity; } else { ReadyAmmo = ammoDifference; } break; case 2: if (ammo > magCapacity) { ReadyAmmo = magCapacity; } else { ReadyAmmo = ammoDifference; } break; case 3: if (ammo > magCapacity) { ReadyAmmo = magCapacity; } else { ReadyAmmo = ammoDifference; } break; case 4: if (ammo > magCapacity) { ReadyAmmo = magCapacity; } else { ReadyAmmo = ammoDifference; } break; case 5: if (ammo > magCapacity) { ReadyAmmo = magCapacity; } else { ReadyAmmo = ammoDifference; } break; default: ReadyAmmo = 0; break; } } // Use this for initialization void Awake () { Text_Ammo_text = Text_Ammo_.GetComponent<Text>(); Text_Clips_text = Text_Clips_.GetComponent<Text>(); PlayerScript = Player_.GetComponent<Player> (); ChangeWeapon_Set (curWeapon); iCH = curWeapon; SetWeapon (); SetAmmo (); } // Update is called once per frame void Update () { Text_Ammo_text.text = "Ammo: " + ReadyAmmo +"/"+ ammo + ""; Text_Clips_text.text = "Clips: " + clips + "/" + magCapacity + ""; ammoTrack (); //SetWeapon (); } }
Thank you, ok I gave that a test run and I was going to use something similar to that; However it's only one issue as it stands. I currently have one button responsible for this task of changing weapons forward cycle. If I had a button for each weapon then your method along with the other method I thought of would work just fine. I need to do this with one button and track the last used weapon that way when the instance occurs again the iteration will start from the last used weapon.
I don't think you even need a loop, surely you only need to deactivate the players current weapon, and active the next, only dealiong with 2 GO's; not the wholearray of them Code (CSharp): void CycleWep (){ array[current].setActive(false); current = (current+1)%array.Length; array[current].setActuve(true); }
Works great Now I just have to add the rest of things to do. so I ask is it the modulus addition that automatically makes it reset to zero?
there no need to manulaly reset to zero % length wraps the value automatically to the length of the array