how to access to a component previously removed and readded
This is my problem, i'm trying to make work an NGUI component called typewritereffect for simulate the typewriter on a string. The component write the string as typewriting then i need to remove it and add it the typerwritereffect component to the string to make it work again. But i have a problem, after i remove the component and i add it again there is no way to point again to that component. I have no idea why. This is my code:
public void interazione(){
Destroy (main_label_obj.GetComponent<TypewriterEffect> ());
main_label_obj.AddComponent<TypewriterEffect> ();
NGUITools.SetActive (interaction_button, false);
type_component.charsPerSecond = 25;
main_label_obj.GetComponent<TypewriterEffect> ().ResetToBeginning ();
EventDelegate.Add (main_label_obj.GetComponent<TypewriterEffect> ().onFinished, function_test);
main_label.text = testi [counter_dialogue];
}
so..i can easily add an eventdelegate, or change the charspersecond prop to the original component, but if i do that after the component replacement nothing happen, any idea why? thank you all
I'm having trouble fully understanding your question but.... Do you NEED to destroy the component? Would it be possible for you to just set it to inactive as you are the NGUITOOLS?
thank you for your help, yes i have to destroy it cause it works just one time, so if i replace my text string it doesn't work anymore, i have to destroy the component and make a new one but i can't work on it, have no idea why
Try to apply any changes after the destroy on the next frame
public IEnumerator interazione()
{
Destroy(main_label_obj.GetComponent<TypewriterEffect>());
yield return null;
main_label_obj.AddComponent<TypewriterEffect>();
NGUITools.SetActive(interaction_button, false);
type_component.charsPerSecond = 25;
main_label_obj.GetComponent<TypewriterEffect>().ResetToBeginning();
EventDelegate.Add(main_label_obj.GetComponent<TypewriterEffect>().onFinished, function_test);
main_label.text = testi[counter_dialogue];
}
Quite bad practice to destoy a component and re create it, make sure there is no reset function on the component.
doesn't work... sadly i asked to the developer of NGUI and he said "you can always AddComponent() to reset it after it finishes.", so i suppose remove it and it again it's the only way, i just don't understand why unity have problem to identify the new component
Probably has to do with the initialization of the Component or some kind of registering it, don't know exactly. The above method works for a Collider probably will work for your component too.
Your answer

Follow this Question
Related Questions
How can I AddComponent() now? 3 Answers
Problem with throwable script in sphere 0 Answers
Multiplayer tutorial 2 Answers
Image Effects missing from components menu? 0 Answers
Not using GetComponent during OnCollisionEnter or OnTriggerEnter 1 Answer