Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by wolgy88 · Aug 31, 2015 at 02:02 AM · button2d game

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.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image binaryspace1010 · Aug 31, 2015 at 02:08 AM 0
Share

Hey wolgy88, I have recently been trying to solve this (and have succeed to an extent) so I'll get you some code written :)

avatar image binaryspace1010 · Aug 31, 2015 at 02:22 AM 0
Share

@wolgy88 I've read your question, and I'm gonna try to solve it ;)

avatar image wolgy88 · Aug 31, 2015 at 02:26 AM 1
Share

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

avatar image binaryspace1010 · Aug 31, 2015 at 02:45 AM 0
Share

@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!

avatar image binaryspace1010 · Aug 31, 2015 at 03:26 AM 0
Share

@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!

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wolgy88 · Aug 31, 2015 at 05:40 PM 0
Share

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.

avatar image wolgy88 · Sep 01, 2015 at 03:32 PM 0
Share

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.

avatar image IHackedDeath · Sep 01, 2015 at 05:31 PM 1
Share

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.

avatar image wolgy88 · Sep 01, 2015 at 05:51 PM 0
Share

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#

avatar image binaryspace1010 · Sep 01, 2015 at 06:13 PM 0
Share

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;

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges