2
\$\begingroup\$

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 Screenshot 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;
}
\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

That is a very slow method and unreliable in case you misspell some naming. Or just rename something. Use references to your SpriteRenderers and simply use them when you need. For example place this in your character code.

[SerializeField] private BodyPartsSpriteRenderers _bodyPartsSpriteRenderers;

// Use example
private void Start()
{
    this._bodyPartsSpriteRenderers._Head.sprite = // Some head sprite
}

[System.Serializable]
public class BodyPartsSpriteRenderers
{
    [SerializeField] private SpriteRenderer _head;
    public SpriteRenderer _Head     { get { return this._head; } }

    [SerializeField] private SpriteRenderer _leftArm;
    public SpriteRenderer _LeftArm  { get { return this._leftArm; } }

    [SerializeField] private SpriteRenderer _rightArm;
    public SpriteRenderer _RightArm { get { return this._rightArm; } }

    [SerializeField] private SpriteRenderer _legs;
    public SpriteRenderer _Legs     { get { return this._legs; } }
}

Specific for your case:

public void EquipItem(Equipment newItem)
{
    switch (newItem._BodyPart) {.........
        case BodyPart.Legs:

            this._bodyPartsSpriteRenderers._Legs.sprite = newItem.icon;

        break;
    ..... }
}

Don't forget to assign them in the inspector or you will get NullReferenceException.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks alot! Switch-Case definitely makes sense here aswell, works exactly as i wanted it to :) \$\endgroup\$
    – Veineless
    Commented May 6, 2018 at 18:22
0
\$\begingroup\$

If your character have only on SpriteRenderer Component you can try something like

//Player is the root of your player gameobject
TargetSR = Player.GetcomponentInChildren<SpriteRenderer>();
TargetSR.sprite = newIcon;
\$\endgroup\$
1
  • \$\begingroup\$ The problem is that my Player Gameobject has a hierarchy of children which each have a spriterenderer (as seen in the screenshot, for example Hand_L and Hand_R both have a spriterenderer) \$\endgroup\$
    – Veineless
    Commented May 6, 2018 at 15:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .