how to get a SpriteRenderer from a child in a prefab from another script
so i am trying to make this script when you throw something it goes back to ur hands. But im doing with in a SpriteRenderer but i cant seem to get the SpriteRenderer Component. Can somebody please? help keep in mind a am sorta new sorry if this hard to read im kinda dumb.
Basic formula is: SpriteRenderer someSpriteRenderer = someGameObject.GetComponent<SpriteRenderer>();
Answer by jeffstaples1982 · Jul 29, 2021 at 05:25 AM
@NicksThicc No reason to call yourself dumb, we were all new once, I'm still new to Unity. :) Seeing as there are no answers Im going to throw something out that may or may not help, but it might get you going in the right direction.
This line gets the sprite image of a child object in a prefab. I was stuck in a situation similar to yours for a while...not sure how to access the SpriteRenderer but its probably just as easy as replacing Image with SpriteRenderer, Sprite with SpriteRenderer and removing the .sprite at the end. The 0 in GetChild is the index of the child in relation to the parent in the hierarchy.
Sorry if this isn't much help, but I feel something is better than nothing.
prefab = GameObject.Find("throwableItem");
Sprite sprite = prefab.transform.GetChild(0).gameObject.GetComponent<Image>().sprite;
maybe this?
SpriteRenderer spriteRenderer = prefab.transform.GetChild(0).gameObject.GetComponent<SpriteRenderer>();
Your answer
![](https://koobas.hobune.stream/wayback/20220613040835im_/https://answers.unity.com/themes/thub/images/avi.jpg)