So, I have a series of gameobject instances all with a script on them. When i select one of these instances a black circle appears AND a UI element should be instantiated with some stats. It works if there is only one instance of the gameobject without any issues. When more object are added and selection changed from one to the other, the circle works perfectly fine, but the UI instantiate only works for the most recently instantiated of the objects. Here is the code:https://hatebin.com/oihkmmbmeq This is the function on the Tower(the object that has more instances):
private void Selected_Change()
{
if (IsSelected)
{
line.enabled = true;
GameObject.FindGameObjectWithTag("UI").GetComponent<UI>().Spawn_Tower_Panel(this.gameObject, 150, "Radius: " + XRad + "\nValue: " + 150 + "\nRotSpeed: " + 7f + "\nDamage: " + Damage + "\n UpPrice" + UpPrice);
}
else
{
line.enabled = false;
GameObject.FindGameObjectWithTag("UI").GetComponent<UI>().Despawn_Tower_Panel();
}
}
And this is the code in the UI script:
public void Spawn_Tower_Panel(GameObject tower,int Value, string Stats)
{
Tower_Panel_Inst = Instantiate(Tower_Panel, Tower_Panel.transform.position, Tower_Panel.transform.rotation);
Tower_Panel_Inst.transform.SetParent(this.transform, false);
Selected_Object = tower;
Tower_Panel_Inst.transform.Find("TowerTypeBackground").transform.Find("TowerType").GetComponent<Text>().text = tower.name.Remove(tower.name.Length - 7);
Tower_Panel_Inst.transform.Find("Values_Stuff").GetComponent<Text>().text = Stats;
TempInt = Value;
}
public void Despawn_Tower_Panel()
{
Destroy(Tower_Panel_Inst);
}
public void Scrap_Tower()
{
TempInt = Mathf.RoundToInt(TempInt * 0.5f);
CreditsChange(TempInt);
Destroy(Tower_Panel_Inst);
Destroy(Selected_Object);
TempInt = 0;
}
public void Upgrade_Tower()
{
if(Selected_Object.name == "Tower_Cannon(Clone)")
{
int temp = Selected_Object.GetComponent<Tower_Cannon>().Upgrade_Price();
if(Materials - temp > 0)
{
CreditsChange(-temp);
Selected_Object.GetComponent<Tower_Cannon>().Upgrade();
}
Destroy(Tower_Panel_Inst);
}
}
https://youtu.be/JN1IgFd_qyU This is a video that shows what happens, and what the problem is. Thank you for taking the time to look into this