Add component to a gameobject created through code.

SpriteRenderer renderer = go.AddComponent();

This script is supposed to add a spriterenderer to my “go” gameobject, but it doesn’t. I tried with this code also :

SpriteRenderer renderer = gameObject.AddComponent();

But it adds spriterenderer to the object the script is attached to, and not to my “go” gameobject. I can’t attach the script to my “go” gameobject to attach the script. How do I do ?

Try

go.gameObject.AddComponent();