AddComponent() throwing NullReferenceException

NetworkTransformChild ntc = gameObject.AddComponent(); = child.transform;
ntc.enabled = true;

I’m trying to add a NetworkTransFormChild component to my player on runtime which targets a child which is created on runtime as well. However, Unity throws me a nullreference error before the target has been set. Everything else works fine, since the second time unity goes through the script, the target has indeed been set. (with this script it gives 1 error for each child, if I comment out the target defining line it keeps on giving errors)

So my main question is: Is there a way to set the target of the component before the component is actually created? Or is there some other way around this error?

AddComponent is used like this:


//I'm not sure about Unity Script, but it looks like you're using C#.

It’s giving you a null error because the ‘ntc’ doesn’t exist, that would be my best guess.

I found a solution that worked for me.
I hope it is still usefull for you and otherwise I hope it is usefull for others that came across of this thread.