3
\$\begingroup\$

I have the following code, in a menu where you can chose the armor and weapon of a character:

private List<string> armors = new List<string> { "armor1", "armor2", "armor2" };
private List<string> weapons = new List<string> { "weapon1", "weapon2", "weapon3", "weapon4", "weapon5", "weapon6", "weapon7", "weapon8", "weapon9" };

private int  armor_index = 0;
private int weapon_index = 0;

public GameObject avatar_body;
public GameObject avatar_weapon;

void Update() {
         if (Input.GetKeyDown(KeyCode.RightArrow)) nextArmor();
    else if (Input.GetKeyDown(KeyCode.LeftArrow))  previousArmor();
    else if (Input.GetKeyDown(KeyCode.DownArrow))  nextWeapon();
    else if (Input.GetKeyDown(KeyCode.UpArrow))    previousWeapon();
    else if (Input.GetKeyDown(KeyCode.Return))     loadLevel();
}

void nextArmor() {
    if (armor_index < armors.Count - 1)
        armor_index += 1;
    else
        armor_index = 0;

    setArmor();
    setWeapon();
}

void previousArmor() {
    if (armor_index > 0)
        armor_index -= 1;
    else
        armor_index = armors.Count - 1;

    setArmor();
    setWeapon();
}

void nextWeapon() {
    if (weapon_index < weapons.Count - 1)
        weapon_index += 1;
    else
        weapon_index = 0;

    setWeapon();
}

void previousWeapon() {
    if (weapon_index > 0)
        weapon_index -= 1;
    else
        weapon_index = weapons.Count - 1;

    setWeapon();
}

void setArmor() {
    avatar_body.GetComponent<Animator>().runtimeAnimatorController =
        Resources.Load("AnimationControllers/player/"+armors[armor_index]+"/body" ) as AnimatorOverrideController;
}

void setWeapon() {
    avatar_weapon.GetComponent<Animator>().runtimeAnimatorController =
        Resources.Load("AnimationControllers/player/"+armors[armor_index]+"/"+weapons[weapon_index] ) as AnimatorOverrideController;
}

The problem is when I start switching between the weapons, sometimes the sprite showing does not update. Inspecting the GameObject, it is weird, because the Animator Component has successfully updated, but the Sprite Renderer still shows a fixed sprite from the last Animator.

Here are 2 examples:

Here we see that the animator is set for the weapon Gladius, and that the Sprite renderer is looping through all the frames of that animation

enter image description here

Here we can see that even though we have moved to the Flanged mace animator, the Sprite Renderer is still stuck on a sprite of Gladius

enter image description here

The problem is hard to recreate, because it happens just SOMETIMES. If I were to switch back to gladius and then once again back to flanged_mace...

enter image description here

Now it works... but I didn't change anything.

What is going wrong??

\$\endgroup\$

0

You must log in to answer this question.

Browse other questions tagged .