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 matta9001 · Oct 10, 2014 at 11:48 PM · javascriptvariables4.6

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 () {
 
 }

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

3 Replies

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

Answer by Eric5h5 · Oct 11, 2014 at 12:02 AM

Use an array or List.

Comment
Add comment · Show 2 · 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 matta9001 · Oct 11, 2014 at 01:27 AM 0
Share

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

avatar image Cherno · Oct 11, 2014 at 01:43 AM 0
Share

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.

avatar image
1

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 () {
     
     }
 }
 
Comment
Add comment · Show 1 · 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 matta9001 · Oct 11, 2014 at 01:27 AM 0
Share

Thank you for the help

avatar image
0

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).

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

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

31 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 avatar image avatar image

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


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