Hi!
I want to get a reference to the PunchPlayer:

Which is a Game Object with a Sprite Renderer:
I’m using this code to do it in the PlayerController class:
SpriteRenderer punchSprite;
protected override void Awake()
{
base.Awake();
life = GetComponent<Life>();
punchSprite = GetComponentInChildren<SpriteRenderer>();
}
But I get a reference to the Player:
I want to show the sprite in PlayerPunch game object after the player hits a punch.
How can I do it? Maybe, I need to change everything. I’m newbie.
Thanks!
The best way is to make the punchSprite field public and then just drag the sprite renderer into it in the inspector.
If you really want to get the child via code though, you can do this:
punchSprite = transform.GetChild(0).GetComponent();
The reason GetComponentInChildren is giving you the parent sprite is that it checks its own game object first before checking the children.
Change this:
SpriteRenderer punchSprite;
To this:
[SerializeField] SpriteRenderer punchSprite;
Then in the Inspector of that script, drag and drop the PlayerPunch game object onto that field. It will automatically pick the SpriteRenderer on that object. From then on, this reference is serialized on that MonoBehaviour and it just exists.
That is, until you a) unassign the reference or b) remove the SpriteRenderer component or c) destroy the PlayerPunch object. But you can change the hierarchy of the player (eg move PlayerPunch to some other child object) without losing that reference.