My components are disappears
I am instantiating a prefab and adding a component to it after it is instantiated. I know that it works because in the added components Start function I output a hello message. But later when the object collides into my player, I call a function from the added component of the collided game object to output a message but I receive a null exception reference. Looking at the hierarchy of the Instantiated game object it does not have my added component?
Any ideas on what could be problem? I am using the latest version of Unity
Answer by Statement · Oct 11, 2015 at 04:07 PM
I call a function from the added component of the collided game object to output a message but I receive a null exception reference.
Sounds like your component have forgot to set a variable.
Looking at the hierarchy of the Instantiated game object it does not have my added component?
Maybe you hid it using hideFlags so it does not show in inspector. Most likely you called AddComponent on the wrong game object. Example of how you could perhaps got it wrong:
var clone = Instantiate(prefab);
gameObject.AddComponent<HelloWorld>();
// ... but should have been ...
clone.AddComponent<HelloWorld>();
Your answer
Follow this Question
Related Questions
Start coroutines when the class is not a MonoBehaviour 1 Answer
Why isn't this instantiation cast working? 1 Answer
Game Manager can't decide what the instance of the object is despite just making an instance of it 1 Answer
unity don't recognise the elements (0, 1, 2 etc...) what can i do ? 0 Answers
While instantiating getting object reference not set for no reason? 1 Answer