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
0
Question by Lineout · Nov 14, 2015 at 12:43 AM · hashtable

Why does my Hashtable try to return an object value?

 Hashtable weaponInformation = new Hashtable();
 
 weaponInformation.Add("BulletSpeed", 250);
 weaponInformation.Add("GrenadeSpeed", 200);
 weaponInformation.Add("MissileSpeed", 125);


This is my current code to set values. The problem I am having is when I try to read my data.

 projectilePrefab.GetComponent<Rigidbody2D>().AddForce(turret.transform.up * weaponInformation[(currentWeaponName + "Speed")]);

It is casting the error: Operator '*' cannot be applied to operands of type 'Vector3' and 'object'

When I read the Hashtable values I want it to return an int not an object. If you could, also explain why it would even try to return an object?

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 joemane22 · Nov 14, 2015 at 05:38 AM

Because the Hashtable is made to receive any type of object so to get an object of type int back you need to do an implicit conversion

 projectilePrefab.GetComponent().AddForce(turret.transform.up * (int)weaponInformation[(currentWeaponName + "Speed")]);

As long as you are sure the object is an integer this will work. Alternatively you could use a Dictionary. It is the same thing as a Hashtable except it only works with one data type. you must add the reference

 System.Collections.Generic;

Then declare the dictionary you want like this

             //key, value 
 Dictionary<string, int> weaponInformation = new Dictionary<string, int>();

Then use it as you were using it before without the implicit conversion, this will be much safer in the long run.

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 MilkerName · Jun 09, 2019 at 03:57 AM 0
Share

Thank you so much! It helped a lot. :D

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

35 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

Related Questions

Hashtable Deserialization & How to create a reference variable to a field obtained through reflection 0 Answers

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

Null reference on dictionary with Unity's Text Localisation tutorial 0 Answers

[Simple] How to retrieve the Hashtable value, stored in a list? 1 Answer

How can I check for an inputted word against a list of words in a text file? 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