- Home /
Question by
yanis010 · Sep 13, 2014 at 09:06 PM ·
c#gameobjectcomponent
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 ?
Comment
Why do you need to add the script, can't you have it disabled and then from your other game object enable it?
Your answer
![](https://koobas.hobune.stream/wayback/20220613162924im_/https://answers.unity.com/themes/thub/images/avi.jpg)