Hey everyone! how do you get the specific number of a string in an array?
String[] allUnits;
foreach (string unit in allUnits){
// create button for each unit....
GameObject button = Instantiate(ButtonToSpawn, SpawnhereinGUI.position, SpawnhereinGUI.rotation);
button.transform.parent = SpawnhereinGUI;
}
allunits is shown in inspector like:
allUnits:
0
1
2
3
4
5
6…
for as many objects are in the Array, how would I get the number of a GameObject?
if I wanted to get each number?
foreach (string unit in allUnits){
GameObject button = Instantiate(ButtonToSpawn, SpawnhereinGUI.position, SpawnhereinGUI.rotation);
button.transform.parent = SpawnhereinGUI;
ButtonSelectUnit ButtonSelectUnitCS;
ButtonSelectUnitCS = button.GetComponent<ButtonSelectUnit >();
ButtonSelectUnitCS.thisButtonsNumber // ?? how to get this for each button?
}
//if this is the sixth button to be dynamically instantiated, how do I tell the button script what
number in the array the game-object belongs to?
so each button that gets instantiated has the number to the string it belongs to in the array?
the 6th button, it would be number 6, the 7th button, it would be number 7…
hey, yea its a bit hard to explain , I know arrays start at 0, I’m trying to get the element number of that string.
for each character in the array it creates a button, regardless of how long the array is. each button has an information script on it, that’s filled when created, if it’s the 3rd character I own, I need to tell that button its the third character I own.
I’ve done this type of thing before with dozens of buttons on the window. The method I used was to set gameObject.name to the desired number. So, when you’re creating the button just set the button name to the index from the array. When the button is pressed, you can use int.Parse(gameObject.name) to get the number back and use that to reference your array. Does that fix it for you?
i think this solution here should do what you are searching for:
//this goes on your ButtonPrefab, must be in seperate file:
public class UpgradeButtonBehaviour : MonoBehaviour {
public UnitInfo Unit;
private LoadUnitsForUpgrade upgradeManager;
private int index;
public void Init(string UnitJSON, LoadUnitsForUpgrade upgradeManager, int index)
{
Unit = UnitInfo.FromJson(UnitJSON);
this.upgradeManager = upgradeManager;
this.index = index;
}
//this could be your click callback to save it:
public void SaveUnit()
{
upgradeManager.SaveUnit(UnitInfo.ToJson(Unit), index);
}
//this could be your click callback to upgrade the unit:
public void Upgrade()
{
//do whatever has to be done to upgrade your unit or save it or whatever
}
}
//change your current class to this:
public class LoadUnitsForUpgrade : MonoBehaviour {
public List<UnitInfo> Units = new List<UnitInfo>();
public GameObject ButtonPrefab;
public Transform SpawnHereInGUI;
public void CreateNewButton(int index) {
var newButton = Instaniate(ButtonPrefab, SpawnHereInGUI);
newButton.GetComponent<UpgradeButtonBehaviour>().AssignUnit(Units[index], this, index);
}
public void SaveUnit(string UnitString, index)
{
Units[index] = UnitString;
}
}
//this can be anywhere as it is only a data container:
[System.Serializable]
public class UnitInfo{
public enum UnitType {Warrion, Archer, Cannon, General}
//add Unit description variables here:
public string Name;
public UnitType Type;
public int Level;
public static UnitInfo FromJson(string jsonString)
{
return JsonUtility.FromJson<UnitInfo>(jsonString);
}
public static string ToJson(UnitInfo unit)
{
return JsonUtility.ToJson(unit, true);
}
}
I’d stronly suggest to at least use the JSON approach with a serializable data class to hold information as this will help you to easily expand your data without any formatting issues. (You don’t have to reinvent the wheel here)
This approach should also provide more flexibility to save different kinds of data/upgrades and so on.
DISCLAIMER: Script not tested and written in notepad, may contains smaller errors/typos/syntax-issues;