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
1
Question by slanden · Nov 09, 2015 at 09:31 PM · c#dictionary

dictionary of functions?

In c# this works:

 Dictionary<int, System.Action> functions = new Dictionary<int, System.Action>();

I'm using System.Action because my functions return nothing and take no parameters

 void Function1()
 {
     Debug.Log("This is Function1.");
 }
 //add a function to the dictionary with key 1
 functions.Add(1, Function1);

 //call it
 functions[1]();
 
 
 //    console output:
 //"This is Function1."

However, in a unity c# script I'm getting an invalidkey error. What am I doing wrong?

EDIT: I figured out what was wrong with my code. I'm using a getter and setter, and in the set block I wasn't setting the variable to the keyword "value"

 private int _funcNum
 public int functionNumber
 {
     get
     {
         return _funcNum;
     }
     set
     {
         _funcNum = functionNumber; //This should be: _funcNum = value;
         DoFunction(_funcNum);
     }
 }

Comment
Add comment · Show 3
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 gjf · Nov 09, 2015 at 05:35 PM 0
Share

there's not enough info here to help you - try showing the code of adding the key too...

avatar image slanden gjf · Nov 09, 2015 at 06:13 PM 0
Share

I edited the post to include more info.

avatar image Cherno · Nov 10, 2015 at 12:01 AM 0
Share

If all else fails, you can always store the function name as a string and use Invoke to call it.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by gjf · Nov 09, 2015 at 09:29 PM

really not sure why it's not working for you - i tried the following and got the expected results (including the error for the entry which didn't exist)

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ActionTest : MonoBehaviour
 {
     Dictionary<int, System.Action> functions = new Dictionary<int, System.Action>();
 
     void Start()
     {
         functions.Add(64, Function0);
         functions.Add(57, Function1);
 
         functions[57]();
         functions[64]();
         functions[3]();
     }
 
     private void Function0()
     {
         Debug.Log("Function 0");
     }
 
     private void Function1()
     {
         Debug.Log("Function 1");
     }
 }

in future, please also include the complete error message.

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 slanden · Nov 10, 2015 at 01:46 PM 1
Share

Thank you gjf, you getting yours to work helped me figure out it had to be in my get/set function because I didn't include that before.

I'll remember to post the error next time as well.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Getting KeyNotFoundException and I can't understand why 1 Answer

Is it possible to shuffle a Dictionary? Any other shuffleable data structure we can access both by key and index? 2 Answers

Dictionary doesn't find value when using new object as key 2 Answers

Assigning a script as a game object in Dictionary 1 Answer

Unity is reseting Dictionary when game is playing 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