- Home /
Best way to handle a lot of variables
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 () {
}
Answer by Eric5h5 · Oct 11, 2014 at 12:02 AM
Use an array or List.
Thanks, I taught myself unity so i didnt really learn all essential parts of coding. I really should but im just a wee child with no available classes. So thanks
As someone who taught himself US and C# via Unity (badly ;)), all I can say is that even without professional tutoring, after having a very basic understanding on the syntax or a program$$anonymous$$g language, it is very much worth it to get a good introductory book,, even if it's "C# for Dummies" or something, because they have a broad spectrum of basic information which you can use as a jump-off point for further research.
Answer by bubzy · Oct 11, 2014 at 12:07 AM
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 () {
}
}
Answer by Cherno · Oct 11, 2014 at 01:26 AM
If you want more than one variable per weapon (I assume you also need damage, ammo capacity etc.) you can create a simple custom class for your guns.
public class Weapon {
public var weaponName : String;
public var AmmoMax : int;
public var AmmoCur : int;
public var sprite : Sprite;
}
You can create a new gun by code like this
Weapon newWeapon = new Weapon();
newWeapon.weaponName = "Desert Eagle";
newWeapon.ammoMax = 7;
You cna also make the class a Monobehavior so you can just add it as a script to an actual weapon object/prefab.
As for the functions, you could use an Array or List as suggested, or a Dictionary (C# only).
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Changing a variable on a different object 2 Answers
Making a List for Inventory System, Got an error in finding constructors and variables 1 Answer
How would you get one variable to be directly correlated to another variable? 1 Answer
Mob Spawner, choosing a random mob 2 Answers