Skip to main content
2 of 3
Added screenshot

Finding SpriteRenderer of specific Child Gameobject

So i'm trying to work on my Equipment System to also change the appearance of the sprites on my Character once i change the equipped item. My animations are setup in limbs so all i would have to do (i think?) is change the actual sprite of the specific limb skin i want to change (e.g. The Shoes), since the animation should still work independent of what the sprite is. However the problem i'm having is that my equipment manager is on a seperate gameobject than the character is and i don't seem to be able to find the SpriteRenderer components (Null pointer reference). My current function looks like this: Obviously the commented parts are irrelevant for my question, and i will eventually make the gameobject search more specific to which item piece i'm equipping. For clarification, the Equipment Manager is on my GameManager object and the sprites are on the Player Object and its' children.

public void EquipItem(Equipment newItem)
{
    //int slotIndex = (int)newItem.equipSlot;
    //Equipment oldItem = null;

    SpriteRenderer targetSR;

    //if(currentEquipment[slotIndex] != null)
    //{
    //    oldItem = currentEquipment[slotIndex];
    //    inventory.AddItem(oldItem);
    //}

    //if (onEquipmentChanged != null)
    //{
    //    onEquipmentChanged.Invoke(newItem, oldItem);
    //}
    //currentEquipment[slotIndex] = newItem;


    targetSR = GameObject.Find("Mage5/Hip/Body/Hand_L").GetComponent<SpriteRenderer>();
    targetSR.sprite = newItem.icon;
}