Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by UrielKane · Jun 26, 2016 at 07:21 PM · inventoryreferencingvariable name

How select between diferent variables from a referenced script with one variable

Hi there to everybody!

Well the question is, i have a public class called AmmoScript with obviously is in a separated script. This class have diferent Ammo variables such as pistol, machinegun, rocket, shotgun shells and so on.

And in the other hand i have a WeaponScript with is the main script for the weapons. And i need some versatile or simple way to get one var or another from AmmoScript.

You probably ask yourself why i need the ammo variables in a diferent script and so on. Well thats becouse i'm going to do diferent weapons that share the same Ammo variable. For example a minigun using the same amunition as the assaultrifle or an automatic shootgun have the same as the classic shotgun.

I was thinking in use a string but seems to be to painfull convert a string into a variable name. The other way i think could be using an enum and depending on wich var is selected from there pick on or another var from ammoscript. The last obviously way si to put the ammo var inside weaponscript and look to the floor and fall defeated by the chimera xD.

Thanks in advance for any sugestion!

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

2 Replies

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

Answer by jgodfrey · Jun 26, 2016 at 08:03 PM

We probably need some more details to give you a decent answer. For instance, what are the ammo variables in the AmmoScript? Are they each just a simple scalar variable (such as an INT) or do they represent a more complex object?

Off the top of my head, I might suggest that you store the information in AmmoScript in a Dictionary, where the key could be either 1) a string representing the weapon type ("pistol", 'machinegun", etc), or 2) an ENUM representing the weapon type (weapontype.pistol, weapontype.machinegun, etc) and the value could be the stored information (an INT, a FLOAT, an object, or whatever you need).

With that, you could simply request the info associated with a given key (via a string, an enum, or whatever you made the key).

But, without more info, it's hard to be sure what might be appropriate...

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 UrielKane · Jun 30, 2016 at 04:41 AM 0
Share

Well with the passing of the days I was testing different solutions. And in the end I stay what you recomend, the dictionary. As always when I find something new I take time to experiment with it and see its possibilities. But eventually we could say that I take your advice.

Thank you!

avatar image
0

Answer by UrielKane · Jun 26, 2016 at 08:31 PM

     //Active ammo vars
     static var PistolAmmo : int = 0;
     static var ShotgunAmmo : int = 0;
     static var AssaultAmmo : int = 0;
     static var RocketAmmo : int = 0;
     static var EnergyAmmo : int = 0;
     static var SoulAmmo : int = 0;
     
         // Other Variables
     static var MaxAmmoLvl : int = 1;
     static var Currency : int = 0;
     static var HealKit : int = 2;
     
     //Starting ammo
     public var SPistolAmmo : int = 30;
     public var SShotgunAmmo : int = 0;
     public var SAssaultAmmo : int = 0;
     public var SRocketAmmo : int = 0;
     public var SEnergyAmmo : int = 0;
     public var SSoulAmmo : int = 0;
     
     //Max Amount of ammo vars
     public var MaxPistolAmmo : int = 200;
     public var MaxShotgunAmmo : int = 200;
     public var MaxAssaultAmmo : int = 200;
     public var MaxRocketAmmo : int = 200;
     public var MaxEnergyAmmo : int = 200;
     public var MaxSoulAmmo : int = 200;
     
     public var StartingAmmo : boolean = true;
 
     function Awake () {
         if(StartingAmmo == true){
             PistolAmmo = SPistolAmmo;
             ShotgunAmmo = SShotgunAmmo;
             AssaultAmmo = SAssaultAmmo;
             RocketAmmo = SRocketAmmo;
             EnergyAmmo = SEnergyAmmo;
             SoulAmmo = SSoulAmmo;
         }
     }

Thanks for your answer

This is the entire AmmoScript And now i look again at it. I realize that at least there are static vars, but i dont know if that would be useful for simplify the thing.

As we see its just a bunch of int variables. Wich im tryng to do is a large number of weapons. And the main reason why i need to get less usable ammo than one per weapon. Is becouse it will be a pain in the ass to do for example 30 diferent pickups for ammo.

 if(Input.GetButtonDown("Reload") && FPStatus.IsShooting == false && FPStatus.IsReloading == false){
             if(AmmoScript.PistolAmmo > 0 && WScript.AmunitionLeft < WScript.AmunitionMax){
                 ReloadWeapon();
             }
         }

This fragment is from my weaponscript where you can see why i need this. What i was doing before is making one script per weapon and work fine. But as soon as the number of weapons increase and begins bigger everything become a super mess. Now i want to do a single script for all weapons and i need this problem of the ammo solved in one way or another.

 function ReloadWeapon () {
         if(WScript.AnimatedGun) {
             WScript.AnimatedGun.Stop();
             WScript.AnimatedGun.Rewind("Reload");
             WScript.AnimatedGun.Play("Reload");
         }
         FPStatus.IsReloading = true;
         if(ReloadSFx){
             audioPlayer.PlayOneShot(ReloadSFx);
         }
         yield WaitForSeconds(WaitForReload);
         
         var AmmoToReload : int;
         var TotalAmmo : int;
         
         if(AmmoScript.PistolAmmo >= WScript.AmunitionMax){
             if(WScript.AmunitionLeft == 0){
                 WScript.AmunitionLeft = WScript.AmunitionMax;
                 AmmoScript.PistolAmmo -= WScript.AmunitionMax;
             }
             else{
                 AmmoToReload = (WScript.AmunitionMax - WScript.AmunitionLeft);
                 WScript.AmunitionLeft = WScript.AmunitionMax;
                 AmmoScript.PistolAmmo -= AmmoToReload;
             }
         }
         else{
             if(WScript.AmunitionLeft == 0){
                 WScript.AmunitionLeft = AmmoScript.PistolAmmo;
                 AmmoScript.PistolAmmo = 0;
             }
             else{
                 TotalAmmo = (WScript.AmunitionLeft + AmmoScript.PistolAmmo);
                 if(TotalAmmo >= WScript.AmunitionMax){
                     WScript.AmunitionLeft = WScript.AmunitionMax;
                     AmmoScript.PistolAmmo = (TotalAmmo - WScript.AmunitionMax);
                 }
                 else{
                     WScript.AmunitionLeft = TotalAmmo;
                     AmmoScript.PistolAmmo = 0;
                 }
             }
         }
         FPStatus.IsReloading = false;
         DrawAmmo ();
     }

In the end this is the reload behaviour of my weapon script. Wich is only a magazine reload for now but thats another matter. Well i think its obviously why i need a way to chose one or another ammo variable to work in the weaponscript.

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

59 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 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

How do I let the player pick up an item with left click? 0 Answers

Inventory & Co. suggestioni? 2 Answers

c# Need help with a simple inventory, 3 slot hotbar. 1 Answer

public Inventory not recognized 1 Answer

How can I load a reference to an int into an int array? (c#) 0 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