GetComponent returns null

MyScript script = child.gameObject.GetComponent();

Im having problems with this code, sometimes it gives me the script and sometimes not but returns a null instead. All of the children have the script attached and it seems it kinda randomly finds the script.

Anyone who know what the problem might be?

I’ve never had that problem. Have you ensured that:

a) “child.gameObject” is the gameobject you think it is?
b) that all your intended gameobjects actually have that script?
c) you aren’t removing the script by calling Destroy()?

Yes I am sure, and it sometimes works and sometimes doesnt. MyScript is actually the script calling the method.
So the MyScript on one object tries to get the MyScript of another object. Maybe that could mess things up?

I wouldn’t think so; that doesn’t make sense to me.

If you run a loop (say 10 times) when it fails, will sometimes those actually return the component and sometimes it will return null? Or is this a per-frame thing?

Hmm as far as I can see it looks like it does it in the loop

Are you using 3? I had this problem in 2.6 - but in 3 now I haven’t had it happen once.