Search code examples
c#xna

How to change the texture of a content that has been loaded already?


I got a menu in my game with buttons to select the level, except for the first level button, all the other buttons use a gray texture, as they are "locked". So when you beat the level 1 for example, it returns to the level selecting menu and the level 2 is unlocked, but i want it to use a different texture when it's unlocked, so i tried to add this in my Update method in the main game class but it still using the gray texture:

            if (level2.Unlocked == true)
            {
                level2Button = Content.Load<Texture2D>("GUI\\level2");
            }
            level2Button.Update(gameTime);

Solution

  • Thanks Fuex and roxik0, i used both suggestions to solve it. I created two texture variables and in my button class i added a method to update the textures:

        public void UpdateTexture(Texture2D texture)
        {
            this.texture = texture;
        }
    

    This way before it draws the button it checks if the level is unlocked and update it to use the proper textures:

     if (level2.Unlocked)
                    {
                        level2button.UpdateTexture(level2buttonNormal);
                    }
    level2button.Draw(spriteBatch);