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 Huma · Jun 20, 2012 at 08:11 PM · c#items

Implementation of Items

Hello, I'm pretty new to Unity and just playing around with some stuff and now came to a point where I don't know if the solution I came up with really is a good way to do it.

I want to put items into the game. Static items with fixed attributes. Startin with a Heal Potion, I tried to make it really easy to create new items. My idea was to make a bunch of effects, than create the item and give them these effects with some concrete values. Therefor I create some classes in C#, like "potion" and then create a GameObject for each Potion I want and put the script on it, then give them the objects linked with the effects and some values.

Here is a simple example:

 public class Effect : MonoBehaviour {
     private  List<GameObject> _target;
     
     public Effect() {
         _target = null;
     }
     
     public Effect(GameObject target) {
         _target = new List<GameObject>();
         _target.Add(target);
     }
     
     public List<GameObject> TargetList {
         get { return _target; }
     }
 }

 public class Heal : Effect {
     public int _healing;
     
     public void ApplyEffect(int amount) {
         List<GameObject> targetList = this.TargetList;
         
         foreach(GameObject target in targetList) {
             if( target.GetComponent("playerStats") != null) {
                 target.GetComponent("playerStats").GiveLife(_healing)
             }
         }
     }
 }

 public class Item : MonoBehaviour {
     public int _id;

     public int ItemID {
         get { return _id; }
         set { _id = value; }
     }
 }

 public class ItemPotion : Item {
     public GameObject[] effects;
     public int[] _values;
     
     public void Use() {
         for(int i = 0; i < effects.Length(); i++) {
             effects[i].GetComponent(effects[i].name).ApplyEffect();
         }
     }
 }

Now I have 2 GameObjects, an effect and a potion. Would be easy to create another potion, with more healing maybe. But i think its a very complicated way to do this. Is there a more common way to do this?

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 whydoidoit · Jun 20, 2012 at 08:13 PM 0
Share

You can't really have a $$anonymous$$onoBehaviour derived class with a constructor like that - you can't create instances of $$anonymous$$onoBehaviours directly - you have to use AddComponent.

But I'm not sure why these classes are $$anonymous$$onoBehaviours - they look more like static classes or singletons to me...

avatar image Huma · Jun 22, 2012 at 05:12 PM 0
Share

I'm just translating all the Code I did before from JavaScript to C#. Will test it out as soon as I'm done. -.-

avatar image Loius · Jun 22, 2012 at 08:12 PM 0
Share

This is similar to how I'm handling effects (and it is working nicely). An "Actor" has a "Stats", and an "Effect" has a "Stats", and when the effect is 'applied' it just adds the stats to the actor's stats based on the 'apply$$anonymous$$ethod', so, like, over-time or all-at-once, temporary or permanent, that kind of thing. I will add to Why's recommendation, these should probably be logical classes and not $$anonymous$$onoBehaviour extensions, to make them as easy as possible to use. That way an Item could have an Effect[] effects, which should still be Inspector-editable.

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

check if list contains item with matching string property 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

An OS design issue: File types associated with their appropriate programs 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