Ok so i have a gunapp, and there are a lot of different guns. I made a variable for every one. I don’t know if this is the best way. I have the new 4.6 UI, i want to make a button for every single one, and pressing one of them will set the image to one of them. What is the best way of like doing that.
What my idea was, was to make a function for every single variable, and set it for every one. Problem is would there would be a function for EVERY VARIABLE, and that is a lot of them.
Basically, i want to change the UI.Image to a specific variable out of a huge list. How would i pick one out like that? Or is there a better way without a vast list of variables?
Here is my script if it helps, i just started it
#pragma strict
var gunImage : UnityEngine.UI.Image;
var m4a4 : Sprite;
var m4a4_xray : Sprite;
var m4a4_modern_hunter : Sprite;
var m4a4_desert_storm : Sprite;
var m4a4_faded_zebra : Sprite;
var m4a4_jungle_tiger : Sprite;
var m4a4_bullet_rain : Sprite;
var m4a4_desert_strike : Sprite;
var m4a4_howl : Sprite;
var m4a4_asiimov : Sprite;
var m4a4_urban_ddpat : Sprite;
var m4a4_radiation_hazard : Sprite;
var m4a4_tornado : Sprite;
var m4a4_zirka
function Start () {
gunImage.sprite = m4a4_xray;
}
function Update () {
}
im afraid I don’t use JS so my answer will be in c#
something like this may work for you
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public Sprite[] guns = new Sprite[4];//adjust this value to match the number of guns
enum _gunNames {m4a4,m4a4_xray,m4a4_modern_hunter,m4a4_desert_storm}; //of course fill this out as you wish
public Sprite gunImage; //i couldnt work out the UnityEngine.UI.Image thing
//this method will allow a bit easier reading but it does require that you populate the guns[] array with the textures that match the enum
void Start () {
gunImage = guns[_gunNames.m4a4_modern_hunter];
}
// Update is called once per frame
void Update () {
}
}