Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Lenny991 · Nov 22, 2021 at 04:09 PM · variablescriptableobjectreference

Having a reference to only one variable.

This might be a very confusing question, so i'll try to explain it well.

I have a game, where right now i am making it so that i can deposit pickaxes, axes, fishing rods and all kinds of things to gather resources automatically. So, in the "DepositTool" script i need to actually set the amount of tools i want to be in a certain place, which is where i have the problem. As i have tons of different tools, i want them to be set automatically. (What i mean by that is that i otherwise would need tons of switch case statements which would be really annoying after making more tools).
I know i also could make it an array, but that becomes really confusing because i have to refer to them as indexes not the actual name of the tool

Is there a way to either have an array where i refer to the objects as their actual names or a way to reference only a single variable to change it.

Here is my code if that helps public static ToolSlot pickaxe; public static ToolSlot axe; public static ToolSlot rod; public static ToolSlot net; public static ToolSlot pot; public static ToolSlot weapon;

The ToolSlot is just the tool and how many tools i have.

 void Set(int num){
         //PlayerStats.selectedtool.count = num;
     }

That selectedTool is the thing i'm going for, which is why it's commented out, because it does not exist.

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
0
Best Answer

Answer by Lenny991 · Nov 23, 2021 at 10:42 AM

Hey, if anyone was looking for the answer,

 switch(tool.type){
             case Tool.toolType.pickaxe:
                 i = 0;
                 break;
             case Tool.toolType.axe:
                 i = 1;
                 break;
             case Tool.toolType.net:
                 i = 2;
                 break;
             case Tool.toolType.rod:
                 i = 3;
                 break;
             case Tool.toolType.pot:
                 i = 4;
                 break;
             case Tool.toolType.weapon:
                 i = 5;
                 break;
         }


that is as efficient as i can think to make it.
IF ANYONE HAS A BETTER SUGGESTION PLEASE TELL IT!!!

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

Answer by comiljou · Nov 23, 2021 at 10:57 AM

 You probably just need to use dictionary something like

   private Dictionary<ToolType, int> _toolCount = new Dictionary<ToolType, int>();
     
     void SetToolCount(ToolType tool, int count) 
     {
        _toolCount[tool] = count;
     } 

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 Lenny991 · Nov 23, 2021 at 05:40 PM 0
Share

Haven't tested it yet, i have never heard of a dictionary before. This looks like what i'm looking for, thanks a ton :)

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

139 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

Related Questions

Scriptable object keeps referencing the objects? 1 Answer

variable returns from referenced script as 0 2 Answers

C'# When Taking Variables From Other Scripts, The Values Do Not Update 1 Answer

C# How do I reference a variable from one script in another script 2 Answers

Is it possible to set variables for classes? 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