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 /
avatar image
0
Question by Eh3an · May 02, 2020 at 10:39 PM · itemitem pickup

How can define item type in my item pickup system

Hi,
First let me explain what is a item: An item is a prefab that player can get it in the game and then some value add to his/her health, score,... .

Here is my interface and class (that is an item) that implemented interface:
Interface:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 namespace ArcadeCar
 {
     public interface IItem
     {
         bool isMoney
         {
             get;
         }
 
         bool isHealth
         {
             get;
         }
 
         void OnPickup();
     }
 
     public class ItemEventArgs : EventArgs
     {
         public ItemEventArgs(IItem item)
         {
             Item = item;
         }
 
         public IItem Item;
     }
 }


Class:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 namespace ArcadeCar
 {
     public class AC_Item_Money : MonoBehaviour, IItem
     {
         public bool _isMoney;
 
         public bool isMoney
         {
             get
             {
                 return _isMoney;
             }
         }
 
         public bool _isHealth;
 
         public bool isHealth
         {
             get
             {
                 return _isHealth;
             }
         }
 
         public Transform _pickupEffect;
 
         public void OnPickup()
         {
             //e.g we instantiate the pickupEffect and then destroy this gameobject
         }
     }
 }



Now my question is how can i define item type (e.g health potion or money) and then in "gameController" script based on item type change player values?!

1.png (15.1 kB)
2.png (10.2 kB)
Comment
Add comment · Show 2
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 Hellium · May 02, 2020 at 10:10 PM 0
Share

To answer you: your OnPickup could take a Player as parameter, so your gameController won't have to do anything. Your item would have the responsibility to increase the health / add itself to the inventory / add money ....

avatar image Eh3an · May 02, 2020 at 10:26 PM 0
Share

Thank you, I'll check this out. Now please exit my question from under moderation mode.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Hellium · May 03, 2020 at 10:23 AM

Here is how I would do it:

 using UnityEngine;
 
 namespace ArcadeCar
 {
     public abstract class AC_Item : MonoBehaviour
     {
         private void OnTriggerEnter(Collider other)
         {
             Player player = other.GetComponent<Player>();
             if(player != null)
                 OnCollectedByPlayer(player);
         }
 
         protected abstract void OnCollectedByPlayer(Player player);
     }
 }
 

 namespace ArcadeCar
 {
     public class AC_Item_Money : AC_Item
     {
         [SerializeField] private int amount = 1;
 
         protected override void OnCollectedByPlayer(Player player)
         {
             player.AddMoney(amount);
         }
     }
 }
 

 namespace ArcadeCar
 {
     public class AC_Item_Health : AC_Item
     {
         [SerializeField] private int amount = 1;
 
         protected override void OnCollectedByPlayer(Player player)
         {
             player.Heal(amount);
         }
     }
 }
 

 namespace ArcadeCar
 {
     public enum PotionType { Health, Mana, Rage }
 
     public class AC_Item_Potion : AC_Item
     {
         [SerializeField] private PotionType potionType;
 
         protected override void OnCollectedByPlayer(Player player)
         {
             player.AddToInventory(potionType);
         }
     }
 }
 

 namespace ArcadeCar
 {
     // Facade design pattern
     public class Player : MonoBehaviour
     {
         // Drag & drop the other components
         [SerializeField] private Health health;
         [SerializeField] private Inventory inventory;
         [SerializeField] private Purse purse;
 
         public void AddMoney(int amount)
         {
             purse.AddMoney(amount);
         }
 
         public void Heal(int amount)
         {
             health.Value += amount;
         }
 
         public void AddToInventory(PotionType potionType)
         {
             inventory.AddPotion(potionType);
         }
     }
 }
 

I don't know what your "GameController" is supposed to do, but the less it does, the better.

If you want an entity to display the health or the money, then create specific entities. For instance:

 using UnityEngine;
 using UnityEngine.UI;
 
 namespace ArcadeCar
 {
     public class PursePresenter : MonoBehaviour
     {
         // Drag & drop the other components
         [SerializeField] private Purse purse;
         [SerializeField] private Text;
 
         private void Start()
         {
             // OnMoneyAmountChanged is an `public event Action<int>`
             // invoked each time the amount of money in the purse is changed
             purse.OnMoneyAmountChanged += UpdateUI;
         }
 
         private void UpdateUI(int moneyAmount)
         {
             Text.text = "Money: " + moneyAmount;
         }
     }
 }

Comment
Add comment · Show 6 · 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 Eh3an · May 03, 2020 at 01:46 PM 0
Share

I just say Wow! This is very helpful, not for me but for everyone that want to know more about unity program$$anonymous$$g. So a question, my gameController has a method called ChangeHealth that changes player health, now it's better that i move ChangeHealth method to the Player script or it's place is okay?!

avatar image Hellium Eh3an · May 03, 2020 at 02:42 PM 1
Share

I would definitely advise you to have a dedicated component to manage the health, or at least, move the method in your Player script indeed.

avatar image Eh3an Hellium · May 03, 2020 at 02:44 PM 0
Share

OK, thank you so much for sharing your knowladge. How can i contact with you later?

Show more comments
avatar image Eh3an · May 03, 2020 at 07:10 PM 0
Share

Another thing that is wrong, is that abstract method can't be private, so i should public them. $$anonymous$$ake these method public isn't illegal?!

avatar image Hellium Eh3an · May 03, 2020 at 07:52 PM 1
Share

$$anonymous$$y bad, I fixed my answer. The method should be protected

avatar image
0

Answer by Eh3an · May 02, 2020 at 10:55 PM

I checked Hellium answer, but if i use his method i can't control game from gameController script, this is very important that gameController controls all of game, and this is illegal that i access the player from Item script.
Now i use this way:
New interface:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 namespace ArcadeCar
 {
     public interface IItem
     {
         bool isMoney
         {
             get;
         }
 
         bool isHealth
         {
             get;
         }
 
         void OnPickup();
     }
 
     public class ItemEventArgs : EventArgs
     {
         public ItemEventArgs(IItem item)
         {
             Item = item;
         }
 
         public IItem Item;
     }
 }


New Class:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 namespace ArcadeCar
 {
     public class AC_Item_Money : MonoBehaviour, IItem
     {
         public bool _isMoney;
 
         public bool isMoney
         {
             get
             {
                 return _isMoney;
             }
         }
 
         public bool _isHealth;
 
         public bool isHealth
         {
             get
             {
                 return _isHealth;
             }
         }
 
         public Transform _pickupEffect;
 
         public void OnPickup()
         {
             //e.g we instantiate the pickupEffect and then destroy this gameobject
         }
     }
 }



So now what is your idea about this way, do you have any suggestion?

Comment
Add comment · Show 2 · 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 Hellium · May 02, 2020 at 10:58 PM 1
Share

this is very important that gameController controls all of game

This is a very, very bad idea. Your gameController will become a "God object" with hundreds or maybe thousands lines. Debugging such class will be a nightmare.


$$anonymous$$oreover, having the isHealth and is$$anonymous$$oney is also a very bad idea. What if you want other types of pickups? Will you add new getters to your interface (and thus, to all your classes implementing the interface)?

avatar image Eh3an · May 02, 2020 at 11:40 PM 0
Share

Okay, so what is your idea about this way:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 namespace ArcadeCar
 {
     public class AC_Item : $$anonymous$$onoBehaviour
     {
         static AC_GameController gameController;
 
         public bool is$$anonymous$$oney;
 
         public bool isHealth;
 
         //We can have more boolean (e.g isShield,...) and we can select multiple 
         void OnTriggerEnter(Collider other)
         {
             //Now here we can change health or score from gameController variable
         }
     }
 }
 

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

125 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

Related Questions

How to add item to my inventory when I pick them up? 1 Answer

Item/Buff Selection UI 2 Answers

Picking up flashlight? 0 Answers

How to make sure score can't be increased by picking up same item multipe times--new idea 2 Answers

Best Way to do Regular Items and Weapons/Intractable/Special Items. 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