Addcompont and Change It's Value After Delete another Same Parent type inheritanced Component ,Addcomponent after DeleteAnother Same MotherType Component And Change It's Value
This one function I set it in Awake() After getskillbutton it will detect Component , if button have skill it will delete it, And Add SkillComponent from another Data, And each Skill is inheritance just like public class Skill1 : Skill and public class Skill2 : Skill But i found if I delete Skill and Add anotherSkill ,then the new Skill can't Change it's Value which like "" SkillButton[i].gameObject.GetComponent().AllTarget = Member; """ If the Button have no skill and Just Add anotherSkill , It unexpectedly work,But I still can't figure out
And If I Don't Destroy Previous Let Button contain two Skill , SkillButton[i].gameObject.GetComponent().AllTarget = Member; Only Change first Skill Value
void LoadSkill()
{
SkillButton = new Button[4];
for(int i=0;i<4;i++)
{
SkillButton[i] = canvas.GetChild(1).GetChild(0).GetChild(i).GetComponent<Button>();
if (SkillButton[i].TryGetComponent<Skill>(out Skill LastSkill) == true)
Destroy(LastSkill);
}
for (int i=0;i<4; i++)
{
if (User.Skills[i]!=0)
{
SkillButton[i].interactable = true;
SkillButton[i].gameObject.AddComponent(Skill_Dic[User.Skills[i]]);
SkillButton[i].gameObject.GetComponent<Skill>().AllTarget = Member; //This Row Only Work when the button have No <Skill> in begining
}
else //()
{
SkillButton[i].interactable = false;
}
}
}
,This one function I set it in Awake() After getskillbutton it will detect Component , if button have skill it will delete it, And Add SkillComponent from another Data, And each Skill is inheritance just like public class Skill1 : Skill and public class Skill2 : Skill But i found if I delete Skill and Add anotherSkill ,then the new Skill can't Change it's Value which like "" SkillButton[i].gameObject.GetComponent().AllTarget = Member; """ If the Button have no skill and Just Add anotherSkill , It unexpectedly work,But I still can't figure out
And If I Don't Destroy Previous Let Button contain two Skill , SkillButton[i].gameObject.GetComponent().AllTarget = Member; Only Change first Skill Value
void LoadSkill()
{
SkillButton = new Button[4];
for(int i=0;i<4;i++)
{
SkillButton[i] = canvas.GetChild(1).GetChild(0).GetChild(i).GetComponent<Button>();
if (SkillButton[i].TryGetComponent<Skill>(out Skill LastSkill) == true)
Destroy(LastSkill);
}
for (int i=0;i<4; i++)
{
if (User.Skills[i]!=0)
{
SkillButton[i].interactable = true;
SkillButton[i].gameObject.AddComponent(Skill_Dic[User.Skills[i]]);
SkillButton[i].gameObject.GetComponent<Skill>().AllTarget = Member; //This Row Only Work when the button have No <Skill>
}
else //()
{
SkillButton[i].interactable = false;
}
}
}
Answer by cin31877 · Feb 01, 2020 at 05:25 PM
I found the solution ,change Destroy to DestroyImmediate
Your answer
Follow this Question
Related Questions
Child of child of MonoBehaviour isn't a MonoBehaviour? 0 Answers
C# - creating a list of classes 5 Answers
How to copy script to another GameObject 1 Answer
How can I destroy a prefab clone? 1 Answer