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?!
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;
}
}
}
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?