- Home /
How to load different sprites on a panel based on player level
Hey all I cant post any code here because i don't know where to start. What i have so far is the xp and level logic set on a script so that the player will level up when reaching certain xp totals. All of that works perfectly. I also have a panel that loads up on button click. What im trying to do is to make the image different on the panel depending on the player level. So for example if player is level 1 and presses the "store" button it will load image 1 as the panel sprite (its a 2d game), then if the player is level 2 and presses the same button it will load image 2. Ive tried using arrays but dont know how to tie it to player level. Im not sure if i need to make a button manager script, how to get access to the player level on the player level script or really what the best direction is. Ive watched alot of tutorials and none of them show me what im trying to do. Happy to provide more information or anything you need to assist. I hope my objective is clear here. Thanks in advance to all. :D
Also i have a switch statement to control xp required for next level. Not sure if that is a suitable place to put button onClick event conditions or not and if that even works.
Answer by highpockets · Mar 02, 2019 at 08:13 AM
I think the best solution here would be to make a public array or serialized field private array of playerLevImages[numberOfPlayerLevels], this should be on a script on the panel that you will be changing. Place the images via the editor and the code should look something like:
int level = playerLevel;
image = playerLevelImage;
public image[] playerLevImages = new image[5]; //put quantity of levels
playerLevelImage = playerLevImages[level - 1];
this.image = playerLevelImage;
Hey highpockets The code is definately in the correct direction but not quite there yet. Thanks for your help thus far. I have been able to get access to the currentLevel variable from my Level$$anonymous$$anager script so that part is all good. There are some problems with the playerLevelImage part. Ill add the code as it sits now and then the errors.
using UnityEngine.UI;
public class LevelImages : $$anonymous$$onoBehaviour { private int level = Level$$anonymous$$anager.currentLevel; Image = playerLevelImage; public Image[] playerLevImages = new Image[10]; playerLevelImage = playerLevelImage[level - 1]; this.Image = playerLevelImage;
void Start()
{
}
the currentLevel is not an error as it is pulling from my Level$$anonymous$$anager script. The errors i am getting are all in the playerLevelImage part. What am i missing for this to work? there are 17 errors in total. = are wrong, one of the the ; is wrong after the [level - 1], and also that the name does not exist so i know i need to reference it somewhere. I have added the script to the panel and have no array to put images in so that feels like a logical place to start. Sorry for my noobness.
Your answer
