- Home /
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.
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];
}
}
}
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.
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
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?
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.
Your answer
Follow this Question
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