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 /
avatar image
0
Question by UnityNoob123 · Feb 16, 2018 at 07:48 PM · variablesswitch

Best way to interchange variables of a single script?

Thanks for checking out my question, I have a gun script that is attached to the player with variables that I want to change depending on which gun the player is holding. I only have one gun implemented so far but want to add more and the current way I am exchanging the variables is pretty ugly and long. I am not sure what the best way of doing this but my best guess is to make a script only containing variables on the weapon model, and on a weapon switch load those into the gun script. But I would like to hear any better or more efficient ways of exchanging variables. Thanks for your time.

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

1 Reply

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

Answer by andycodes · Feb 16, 2018 at 09:19 PM

I think you'd really like Scriptable Object's. Brackeys has an amazing video on this that really puts it all in plain English too.

If those don't float your boat, I would also suggest trying out a Strategy Design Pattern . The purpose of this is to provide an easy way to make many things that do the same thing, differently. In plain English: QuickSort and MergeSort both sort a list, but they go about doing it differently. In your case, SMG and Pistol both fire bullets, but may deal damage differently.

To get you started with a basic Strategy Pattern:
IWeapon.cs

 //you'll want an interface as the main type to allow you to swap out the damage of guns.
 public Interface IWeapon {
     //gets and sets the damage the weapon deals.
     public float getDamage();
     public void setDamage(float damage);
     //gets and sets the rate of fire
     public float getRateOfFire();
     public void setRateOfFire(float rate);
     //gets and sets for whether the weapon is automatic.
     public bool getIsAutomatic();
     public void setIsAutomatic(bool isAuto);
 }


Now, lets create a base weapon type that contains the relevant info for a pistol:
Pistol.cs

 public class Pistol : IWeapon {
 // because this implements the IWeapon interface, you also need to include all its methods.
     
     float damage = 10;
     float rateOfFire = 15;
     bool isAuto = false;
 
      public float getDamage() {
         return this.damage;
     }
      public void setDamage(float damage){
         this.damage = damage;
     }
      //gets and sets the rate of fire
      public float getRateOfFire(){
         return this.rateOfFire;
     }
      public void setRateOfFire(float rate){
         this.rateOfFire = rate;
     }
      //gets and sets for whether the weapon is automatic.
      public bool getIsAutomatic(){
         return this.isAuto;
     }
      public void setIsAutomatic(bool isAuto) {
         this.isAuto = isAuto;
     }
 }


When you want to create say, SMG or Rifle now, all you need to do is implement the IWeapon interface.

To use these, a quick code snippet would be like so:

     public IWeapon weapon = new Pistol();
     public Animator playerPoses;
     public gameObject weaponModel;
     
     public someShootingScript _shootingScript; //the actual script that shoots the gun and has the damage per bullet.
     
     void Start() {
         _shootingScript.setModelAndAnimator(weaponModel, playerPoses);
         _shootingScript.setDamage(weapon.getDamage());
         //... and so on
     }
 
     void changeToWeapon(Animator pose, GameObject model, IWeapon weapon) {
         //pretty much just repeat Start()
     }



I hope this helps!

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 UnityNoob123 · Feb 16, 2018 at 11:26 PM 0
Share

Wow thanks so much for taking the time and giving me great multiple answers.

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

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

Why my script isn't work? 1 Answer

How do I get two variables to switch places? 2 Answers

List of reserved variables such as transform and rigidbody 1 Answer

Problem about reading and writing from text files 1 Answer

Accessing variables from android java objects 1 Answer


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