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 zhavens88 · Sep 13, 2021 at 10:24 AM · programmingvariableslistsswapping

Problem with swithing weapons in a list of scriptable objects

First off Im like beginner/intermediate at programming and have A LOT to learn, so im sure this is a terrible way to do things.

I have a set of scriptable objects as weapons. A database script which holds a list of all the weapons. A Player scipt that holds a list of eqquiped weapons. and a playerShoot script which handles the switching weapons.

i was switching weapons with enums before which worked fine but it wouldnt allow for the player picking up new weapons and having those be assigned to 1, 2, or 3 on the keyboard dynamically (i.e first weapon picked up goes to number 1 slot). Thats why i switched to the list of equipped weapons, so i can add to the list upon pickup.

The problem is no matter what, the curWeapon variable is always set to the AsaultRifle scriptable object. i cant seem to change it through inputs. i can change it in the editor before running but its always asaultRifle during run time. There is nothing in update that is setting it every frame. its always that one scriptable object. is it not possible to change this type of variable?

ive included just the important parts of the scripts here because they are pretly clutter with notes and sectioned off pieces of code since im refactoring.

WeaponDatabase.cs

 public class WeaponDatabase : MonoBehaviour
 {
     public List<WeaponScrObj> weapons;
 
 }

Player.cs The list indexes are, 0 = Pistol, 1 = SMG, 2 = AsaultRifle

 public class Player : MonoBehaviour
 {
 

  public WeaponDatabase weaponData;

  public List<WeaponScrObj> equipedWeapons;
  public WeaponScrObj curWeapon;

 public int pistolAmmo;
 public int smgAmmo;
 public int rifleAmmo;

 public int pistolClip;
 public int smgClip;
 public int rifleClip;

 public float accuracy;
 public float range;
 public int damage;
 public int clipSize;
 public float fireRate;
 public float reloadRate;
 
 void Start()
     {
         
         weaponData = GetComponent<WeaponDatabase>();
         equipedWeapons = new List<WeaponScrObj>();
 
         equipedWeapons.Add(weaponData.weapons[0]);
         equipedWeapons.Add(weaponData.weapons[1]);
         equipedWeapons.Add(weaponData.weapons[2]);
         curweapon = weaponData.weapons[0]   
     }
 void Update()
 {
     accuracy = curWeapon.accuracy;
     range = curWeapon.range;
     damage = curWeapon.damage;
     fireRate = curWeapon.fireRate;
     clipSize = curWeapon.clipSize;
     reloadRate = curWeapon.reloadRate;
     

     if (curWeapon == weaponData.weapons[0])
     {
         accuracy = weaponData.weapons[0].accuracy;
         range = weaponData.weapons[0].range;
         damage = weaponData.weapons[0].damage;
         fireRate = weaponData.weapons[0].fireRate;
         clipSize = weaponData.weapons[0].clipSize;
         reloadRate = weaponData.weapons[0].reloadRate;
         weaponData.weapons[0].totalAmmo = pistolAmmo;
     }
     if (curWeapon == weaponData.weapons[1])
     {
         accuracy = weaponData.weapons[1].accuracy;
         range = weaponData.weapons[1].range;
         damage = weaponData.weapons[1].damage;
         fireRate = weaponData.weapons[1].fireRate;
         clipSize = weaponData.weapons[1].clipSize;
         reloadRate = weaponData.weapons[1].reloadRate;
     }
     if (curWeapon = weaponData.weapons[2])
     {
         accuracy = weaponData.weapons[2].accuracy;
         range = weaponData.weapons[2].range;
         damage = weaponData.weapons[2].damage;
         fireRate = weaponData.weapons[2].fireRate;
         clipSize = weaponData.weapons[2].clipSize;
         reloadRate = weaponData.weapons[2].reloadRate;
     }
 }
 }

PlayerShoot.cs

 if (Input.GetKeyDown(KeyCode.Alpha1))
        {
                 player.curWeapon = player.equipedWeapons[0];
 }
 if (Input.GetKeyDown(KeyCode.Alpha2))
        {
                 player.curWeapon = player.equipedWeapons[2];
 }
 if (Input.GetKeyDown(KeyCode.Alpha3))
        {
                 player.curWeapon = player.equipedWeapons[3];
 }




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 DivainUnity · Sep 13, 2021 at 06:54 PM

It seems like you do setting your curWeapon variable every frame on line 65

if (curWeapon = weaponData.weapons[2])

should be double ==

Also, inside your PlayerShoot.cs, check your numbering, seems like you're out of scope there (0, 2 ,3 seems like it should be (0, 1, 2).

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 zhavens88 · Sep 14, 2021 at 12:22 AM 0
Share

Good lord, its always the little things. That did the trick. Thanks so much for being my second eyes here. I wish there was an error or note when you use a single = in an if condition, like "Thats not a question dummy...". Feeling a little sheepish about stating nothing in the update is changing in bold.

Ironically i just answered someones question that had the same issue. just a small symbol was wrong. Thanks Again!!

avatar image DivainUnity zhavens88 · Sep 15, 2021 at 05:25 PM 0
Share

It happens to the best of us so I'm glad to be your second eyes! Please don't forget to vote this answer up

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

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

Multiple Cars not working 1 Answer

Trying to use lists to check for available spawn points 2 Answers

InvokeRepeating help 2 Answers

Swapping Items in List Replaces Value Instead 1 Answer

Inspector Assigned Variables Not In Both Scenes 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