Reputation: 11
I've been tying to instantiate a gameobject prefab which is a button UI type, and change their name so that it has their own identity or so, in my case, i'm trying to change the button name to skill's name
here's my code sample
public GameObject buttonObj; //to instantiate
public GameObject buttonParent;
List<GameObject> gameObjects = new List<GameObject>();
public List<Button> skillButtons= new List<Button>();
private void Start()
{
for(int i = 0; i < skills.Count; i++)
{
GameObject gotemp = Instantiate(buttonObj);
gotemp.transform.SetParent(buttonParent.transform,false);
gameObjects.Add(gotemp);
}
for (int i = 0; i < skills.Count; i++)
{
gameObjects[i].name = skills[i].Name;
gameObjects[i].GetComponentInChildren<Text>().text = skills[i].Name;
skillButtons.Add(gameObjects[i].GetComponent<Button>());
}
}
code for skill part
public class Skill : ScriptableObject
{
public string Name;
public int CooldownTurn = 0;
int cooldownCD;
}
by right all buttons should have their different name with it, but it turns out that only the 1st one has its name while other doesn't have
i know that i should've merge those 2 loops together since they're under the same loop count, but even i tried that at first, it gives me the same outcome, that's why i try to separate it and see if it works
Upvotes: 0
Views: 39
Reputation: 11
so turns out it's a bug from editor script which is a silly mistakes that i've made here's the changes that i've made for that might have to avoid to put a string variable which is near identical to 'name' since it's the name of the gameobject
[CustomEditor(typeof(Skill))]
public class SkillEditor : Editor
{
public override void OnInspectorGUI()
{
Skill selectedSkill = (Skill)target;
selectedSkill.name = selectedSkill.Name = EditorGUILayout.TextField("Skill Name:", selectedSkill.name);
}
Upvotes: 1