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 Brokenarrow · Feb 03, 2016 at 04:34 PM · c#getcomponentvaluedictionaryanother script

Changing a dictionary value from another script

Hi, I'm tring to figure out how to set a dictionary value from another script. I have a GunClass and a InventoryClass. The GunClass handles all shooting mechanics, where the InventoryClass manages the state of the inventory. I keep my inventory items in dictionaries.

In the GunClass I check how many clips (int) I still have left in one of the dictionaries in my InventoryClass, like so:

 if (Input.GetButtonDown("Reload"))
         {
             remainingClips = GameObject.FindGameObjectWithTag("InventoryController").GetComponent<InventoryClass>().playerAmmo[weaponType];
             if (remainingClips > 0)
             {
                 Reload();
             }

This works fine. Reload() decreases remainingClips by 1. However, I also want to decrease the value in the dictionary itself by 1. I tried to do this by adding this line after Reload():

 GameObject.FindGameObjectWithTag("Player Controller").GetComponent<FPSControl>().playerAmmo[weaponType] = remainingClips;

Edit: this is an old snippet: it is supposed to be:

 GameObject.FindGameObjectWithTag("InventoryController").GetComponent<InventoryClass>().playerAmmo[weaponType] = remainingClips;

This however, doesn't do anything. I'm unsure how to handle this problem. Any help would be greatly appreciated.

Comment
Add comment · Show 2
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 dppc · Feb 03, 2016 at 05:10 PM 0
Share

Looks like you're getting the value from one place (playerAmmo dictionary of the InventoryClass component on the "InventoryController" GameObject) and then setting it somewhere else (playerAmmo dictionary of the FPSControl component on the "Player Controller" GameObject). $$anonymous$$aybe you just need to set it in the same dictionary where you got it from?

avatar image Brokenarrow · Feb 03, 2016 at 06:36 PM 0
Share

Woops I copied the wrong code snippet: thats an old version.

 GameObject.FindGameObjectWithTag("InventoryController").GetComponent<InventoryClass>().playerAmmo[weaponType] = remainingClips;

1 Reply

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

Answer by Brokenarrow · Feb 08, 2016 at 12:25 PM

I fixed the problem using a workaround: Instead of changing the value directly, I changed a bool in the target script which adds or changes the dictionary value.

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

70 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

I am very new to programing > I have a script attached to a cloned game-object and need help referancing ..... Any help would be wonderful ^_^ 3 Answers

Reference to value type 1 Answer

C# multiple key/value pairs of various types in a single line of code...? 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