Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by oc007061 · Mar 23, 2014 at 03:42 PM · c#weaponsmousewheel

Changing GUI Texture with mousewheel?

I am working on my weapon HUD, and want to change the Texture with mousewheel. Here is my code.

 using UnityEngine;
 using System.Collections;
 
 public class WeaponHUD : MonoBehaviour {
     public Texture Unarmed;
     public Texture Pistol;
     public Texture Shotgun;
     public Texture Carbine;
     public Texture AK;
     public Texture RPG;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetAxisRaw ("Mouse ScrollWheel") > 0) {
             if (guiTexture.texture = Unarmed) {
                                 guiTexture.texture = Pistol;
             }
         }
     }
 }

Works fine for pistol, as I currently have that set only. My problem is, I can't get the others to work as well. Please help.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Mar 23, 2014 at 04:10 PM

The typical solution to this problem would be to put your weapons into an array. Then you can increment an index:

 using UnityEngine;
 using System.Collections;
 
 public class WeaponHUD : MonoBehaviour {
     public Texture[] weapons;
     int iCurr = 0;
     
     void Update () {
         if (Input.GetAxisRaw ("Mouse ScrollWheel") > 0) {
             iCurr = (iCurr + 1) % weapons.Length;
             guiTexture.texture = weapons[iCurr];
         }
     }
 }
Comment
Add comment · Show 5 · 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 oc007061 · Mar 23, 2014 at 04:18 PM 0
Share

Yeah, thing about that, I don't actually have the guns implemented yet, just the icons. I actually wanted to just see if I could get this working, and I can't so far. I am actually going to be using more guns than there are in that list. Regardless, I appreciate the feedback. Though this brings something to $$anonymous$$d. I know I managed to get something like this working before (actually, exact same thing) but then my hard drive died and I got a new computer. I just totally forgot how I did it.

avatar image robertbu · Mar 23, 2014 at 04:26 PM 0
Share

This code just works with textures. If it more understandable, replace the word 'weapons' with the word 'icons'. To test it:

  • Attach the script to a GUITexture

  • Open the 'weapons' array by clicking on the triangle next to 'weapons'

  • Set the size of the array to the number of icons/textures you have

  • Drag and drop each texture onto a slot in the array

  • Run and use the scroll wheel

avatar image oc007061 · Mar 23, 2014 at 04:31 PM 0
Share

Ahhh, I misunderstood the first time. Tested and works fine! Thank you! I would upvote, but I don't have 15 rep... :( You know, I forgot to ask, how do I make it go backwards by using mousewheel down?

avatar image robertbu · Mar 23, 2014 at 05:19 PM 0
Share

To go down, add this in Update():

 if (Input.GetAxisRaw ("$$anonymous$$ouse ScrollWheel") < 0) {
     iCurr--;
     if (iCurr < 0) iCurr = weapons.Length-1;
 }

If you question is answered, click on the checkmark to the left of the answer to close it out. Thanks.

avatar image oc007061 · Mar 23, 2014 at 05:21 PM 0
Share

Love it! Thanks so much!

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

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Initialising List array for use in a custom Editor 1 Answer

Adding a delay for weapon switching 1 Answer

Flip over an object (smooth transition) 3 Answers


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