Image appearing after clicking a button
So I've done about 2 days worth about of googling so I apologize if this has been answered somewhere else and I couldn't find it. I will also say that I am new to Unity and although I have coding experience with Java, c is still somewhat foreign to me, so a through explanation of any scripts would be appreciated.
I currently have a 2D game where I have a background, a UI image of a robot, and UI image of armor. The buttons say light, medium, and heavy armor and I would like to have the game work so If I press the light button, the image for light armor appears over the image of the robot. Likewise for the medium and Heavy buttons. I can't seem to figure out how to accomplish this.
Hey wolgy88, I have recently been trying to solve this (and have succeed to an extent) so I'll get you some code written :)
@wolgy88 I've read your question, and I'm gonna try to solve it ;)
I appreciate it. I feel like this is a fairly basic thing, so it shouldn't be to much trouble. maybe i'm going about setting it up wrong
@wolgy88 I just want to say, I've run into a bit of a snag, but I'm still working. And it really isn't any trouble! Also, it isn't an easy task, but you came to the right place!
@wolgy88 Hey again, I'm going to take a break and continue tomorrow, i easily worked everything out except for the positioning of the new object. I can't get it to match up with where the robot is. I'll continue tomorrow (for I never back away from a challenge) After doing a bit of checking on how positioning works! Goodnight!
Answer by IHackedDeath · Aug 31, 2015 at 02:21 PM
Hey Wolgy88,
I havn't worked too much on 2d games and sprites but I would imagine that you could save a static public Integer on the robot which when assigned to 0 means the robot has no armor and when it is equal to 1, 2, or 3 then the robot is wearing light, medium, or heavy armor.
After the Integer is setup then I would change the image of the robot to a new made up image of the robot with these armor's or change the position of the armor to be the same as the robot's and possibly become a child of the robot's.
As I said I havn't worked much with 2d games but I believe that this should work, also using static public variables can be a bit troublesome I believe especially when you use many but as a temporary fix or even just a once off use should be fine.
If you provided further information like whether it is sprites or gameobjects you use for the robot and armor etc. then I am more then happy to provide further assistance :)
I hope this helps.
Kind Regards,
IHackedDeath.
So what I did, since I'm new and this was easiest at the time, is I simply created a new ui image for both the robot and armor and attached their image to those. None of these images will be moving so syncing them up is not an issue.
I would love any input on a better way to do this in case I would like to scale it up.
So I have the day off and have had time to think about this. I really like the idea that each button corresponds to a number. Again, I'm not familiar with c# so it would kindof be like an enum class. Not only would this number have an image appear on the screen, but it would also set the values attached to certain armor such as it's statistics. so the pseudo code for this would be something like
if (noArmor button is clicked) Show no image, set armor resistence to x else if (lightArmor button is clicked) Show image light armor, set armor resistence to y and so on. I'm just not sure how this would be written out in code.
Sorry for late comment, been busy and will be for a while.
Your getting the idea :)
There are a lot of ways to go about it, but I like the sound of what your thinking.
I have had a very quick attempt at writing a bit of the code that might prove necessary but I know there will be errors and a bit of adjustment, atleast it is a start though of how I would go about it.
public Sprite armorL;
public Sprite armor$$anonymous$$;
public Sprite armorH;
int coins;
int armorValue;
int speedReduction;
Sprite currentArmor;
Sprite armorType;
int armor (int armorSelected)
{
int aS;
aS = armorSelected;
if (aS == 0)
{
armorValue = 0;
speedReduction = 0;
armorType = null;
}
if (aS == 1)
{
armorValue = 1;
speedReduction = 1;
armorType = armorL;
}
if (aS == 2)
{
armorValue = 2;
speedReduction = 2;
armorType = armor$$anonymous$$;
}
if (aS == 3)
{
armorValue = 3;
speedReduction = 3;
armorType = armorH;
}
if (aS < 0 || aS > 3)
{
return;
}
currentArmor = armorType;
}
void take$$anonymous$$oneyAndChangeArmor ()
{
if (coins >= 5)
{
coins -= 5;
armor (1);
}
if (coins >= 10)
{
armor (2);
}
if (coins >= 15)
{
armor (3);
}
}
Give it a shot and let me know how it goes.
Good luck.
Right, all of that makes sense, I'm still trying to figure out how relate clicking a button to assign a number to as. Also, my image isn't a sprite, it's a UI image, how would that change the code. And again, most of this comes down to my lack of understanding of c#
To change from sprite to UI, you can simply use:
GameObject.Find("THE NA$$anonymous$$E OF YOUR ROBOT UI I$$anonymous$$AGE").GetComponent().sprite = Sprite Armor Image;
Your answer
Follow this Question
Related Questions
How to move character with UI buttons? 0 Answers
Detecting a button click 1 Answer
the button in the bottom of scene don't work 1 Answer
2D toggle on/off button without UI? 0 Answers
UI Text Editing Problem, What's Wrong? Need Anybody to Assist 1 Answer