I have difficulty in understanding how the GetComponent thing works, specially when it involves parents and children.
I’m experimenting with changing the fox’s color when it hits something, but i don’t understand exactly what’s happening.
public SpriteRenderer renderer;
void Start () {
animator = GetComponent<Animator>();
attackers = GetComponent<Attackers>();
renderer = GetComponent<SpriteRenderer>();
}
in this case i managed to have the fox spawn entirely black. My first question is: why did i have to drag and drop the Body’s (fox’s child) Sprite Renderer component to the Sprite Renderer slot (see image below)? Shouldn’t the line “renderer=GetComponent();” find it? I even tried to replace it with GetComponentsInChildren, without success.
Second question - I moved the line
renderer.color = new Color(0f, 0f, 0f, 1f);
to the OnTriggerEnter2D method, and then it stopped having any effect - the fox never gets black, even though i’ve already managed to have it jump over the grave stone and all. What might be the issue?