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 michael96schmidt · Jan 05, 2015 at 11:49 AM · classclasses

Alternative way to have a class to store data

I am quite certain that theres an easier way to have a class which stores data shared throughout multiple classes but I can't figure out how. I'm not quite sure how to put this into words but I want a better way to access data based on an input in a unique class. The way I am currently doing this is using a class with public functions. When I need to I instantiate the class and call the function. Heres an example of what i'm doing:

Accessed Class:

 using UnityEngine;
 using System.Collections;
 
 public class ItemHandler : MonoBehaviour {
 
     public float getDamage(string weapon)
     {
         switch(weapon)
         {
         case "Sword":
             return 1000;
         case "Mace":
             return 2000;
         default:
             return 100;
         }
     }
 }

Using the other class:

 float x = new ItemHandler().getDamage("Sword");


Is there any better way for me to do this so that I don't have to create a new object each time?

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

Answer by weenchehawk · Jan 05, 2015 at 10:35 PM

Almost certainly there is, but there are a number of approaches & I'm not 100% sure I understand what you want to do. Let me start with what I think you are asking for & some possible approaches.

I think you're looking to have an easy way to access general application data - in your example you're getting weapon stats but I'm sure you intent to use it for other things.

Option 1

The first simple improvement you could make is to make getDamage a static function. This will mean you no longer need to instantiate the data storage class

 public static float getDamage(string weapon)
 {
     switch(weapon)
     {
         case "Sword":
             return 1000;
         case "Mace":
             return 2000;
         default:
             return 100;
     }
 }

now you can just call

 float x = ItemHandler().getDamage("Sword");

Option 2

The next thing you can do is consider some structure in your storage method.

 public class Weapon  : MonoBehaviour
 {
   public float Cost;
   public float Damage;
   public float Weight;
 }
 
 public class AvailableWeapons  : MonoBehaviour
 {
   public static Weapon Sword = new Weapon();
   public static Weapon Mace = new Weapon();
 
   void Start()
   {
     Sword.Cost = 20f;
     Sword.Damage = 8f;
     Sword.Weight = 4f;
 
     Mace.Cost = 15f;
     Mace.Damage = 6f;
     Mace.Weight = 3f;
   }
 }

now you can just call

 float x = AvailableWeapons.Sword.Damage;

Option 3

You could have a weapon prefab and simply attach a copy of the Weapon class (from above) to it in the unity editor, then in the inspector fill in the stats, and when you hit somebody with the weapon, you can call something like

 void OnAttack()
 {
   Target.health = Target.health - gameObject.GetComponent<Weapon>().Damage;
 }

This has the added bonus of still working when the player changes weapon.

Personally I'd just go with Option 3

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
0

Answer by Rainbirth · Jan 06, 2015 at 03:41 AM

I recomend you that start working with CSV data for your weapons, and characters, it scalates very quickly if you're trying to attempt to create a RPG game, from my point of view you'll need massive amount of data. I suggest to read some tutorials on using CSV to store your values and use of dictionary or lists.

check out this tutorial. http://forum.unity3d.com/threads/aligning-object-to-surface-normal-with-quaternion-lookrotation.63619/

That way you'll be able to load data for your enemies, weapons, items, etc.

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 Kiwasi · Jan 06, 2015 at 03:53 AM 0
Share

CSV for scalability? You ever tried to add a new field to a CSV after it has been populated with data? Nested fields? CSVs are great for extracting complete data from built application. Not the best for game data, especially during development where data structure changes frequently.

JSON or a X$$anonymous$$L solution is better.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Editting a custom javascript class in the inspector 1 Answer

Auto Commenting lines 1 Answer

Access class variables of selected object 1 Answer

Accessing a variable inside a class inside other script... 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