Hello been trying all day to figer out how to make like a Health Potion i have this Scripts so i can make a "Item" this is my scripts, Code (csharp): using UnityEngine; [CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")] public class Item : ScriptableObject { new public string name = "New Item"; public Sprite icon = null; public bool isDefaultItem = false; public virtual void Use () { // Use the item // Something might happen Debug.Log("Using " + name); } public void RemoveFromInventory () { Inventory.instance.Remove(this); } } Code (csharp): using UnityEngine; public class Interactable : MonoBehaviour { public float radius = 3f; public Transform interactionTransform; bool isFocus = false; Transform player; bool hasInteracted = false; public virtual void Interact() { // This method is meant to be overwritten // Debug.Log(" Interacting with " + transform.name); } void Update() { if (isFocus && !hasInteracted) { float distance = Vector3.Distance(player.position, interactionTransform.position); if (distance <= radius) { Interact(); hasInteracted = true; } } } public void OnFocused (Transform playerTransform) { isFocus = true; player = playerTransform; hasInteracted = false; } public void OnDeFocus() { isFocus = false; player = null; hasInteracted = false; } void OnDrawGizmosSelected() { if (interactionTransform == null) interactionTransform = transform; Gizmos.color = Color.yellow; Gizmos.DrawWireSphere(interactionTransform.position, radius); } } and the Pick up Code (csharp): using UnityEngine; public class ItemPickUp : Interactable { public Item item; public override void Interact() { base.Interact(); PickUp(); } void PickUp() { Debug.Log("Picking up " + item.name); bool wasPickedUp = Inventory.instance.Add(item); if(wasPickedUp) Destroy(gameObject); } } any one ? I need a Ref to the player health i guess but hmm damn cant figer it out i tryed make a new script and Inherent from ItemPick up but noo, and then i whas like maby i can inharet from both my itempick up and my Characterstats? but i dont know how :/
Code (csharp): using UnityEngine; using UnityEngine.UI; public class CharacterStats : MonoBehaviour { // HealthBar. public Slider healthbar; // Player Stats. public int maxHealth = 100; public int currentHealth { get; private set; } public Stat damage; public Stat armor; void Awake() { currentHealth = maxHealth; healthbar.value = CalculateHealth(); } void Update() { if (Input.GetKeyDown(KeyCode.H)) { currentHealth += 15; } } public void TakeDamage (int damage) { damage -= armor.GetValue(); damage = Mathf.Clamp(damage, 0, int.MaxValue); currentHealth -= damage; healthbar.value = CalculateHealth(); Debug.Log(transform.name + " takes " + damage + " damage."); if (currentHealth <= 0) { Die(); } } float CalculateHealth() { return currentHealth / (float)maxHealth; } public virtual void Die () { // Die in some way // This meathod is meant to be overwritten Debug.Log(transform.name + " died."); } }
Is there somewhere you've defined what a HealthPotion is? Where are you implementing the "Use" function of the Item class? Multiple inheritance is not supported in C#. You may want to look into using an Interface instead of a parent class for certain behaviors like Interactable. You can have the player controller look for objects which implement your interface, and call "Interact" on them. That way you won't have every object doing distance checks on the player every frame.
Untested, just for example: Spoiler: InteractableInterface Code (CSharp): // your interactable objects implement this: public interface IInteractable { void Focus(); void UnFocus(); void Interact(); } Spoiler: ExamplePlayer Code (CSharp): using UnityEngine; public class Player : MonoBehaviour { public float viewDistance; public LayerMask interactableLayers; private IInteractable focusedObject; public bool HasFocusedObject { get { return focusedObject != null; } } private void Update() { this.CheckViewTarget(); // interaction input if(Input.GetKeyDown(KeyCode.Space) && HasFocusedObject) { focusedObject.Interact(); } } private void CheckViewTarget() { Ray ray = Camera.main.ViewportPointToRay(Vector2.one * 0.5f); RaycastHit hit; if(Physics.Raycast(ray, out hit, viewDistance, interactableLayers)) { IInteractable interactableObject = hit.collider.GetComponent<IInteractable>(); if(interactableObject != null) { FocusObject(interactableObject); } } else { UnfocusObject(); } } private void UnfocusObject() { if(focusedObject != null) { focusedObject.UnFocus(); focusedObject = null; } } private void FocusObject(IInteractable target) { UnfocusObject(); focusedObject = target; target.Focus(); } } Spoiler: InteractableObject Code (CSharp): // basic component that implements IInteractable explicitly public class InteractableObject : MonoBehaviour, IInteractable { public string name; void IInteractable.Focus() { Debug.LogFormat("{0} was focused.", name); } void IInteractable.Interact() { Debug.LogFormat("{0} was interacted with.", name); } void IInteractable.UnFocus() { Debug.LogFormat("{0} was unfocused.", name); } }
But how do i Get my currentHealth that is in my CharacterStats script in my Item Script qus the i can do like currentHealth += 15; ??
You could do what you're already doing, and on Focus or Interact, pass in the Player object as a parameter. Then in the item class, you can do player.GetComponent<CharacterStats>().AddHealth(itemValue). Maybe like this: Spoiler: InteractableInterface Code (CSharp): // your interactable objects implement this: public interface IInteractable { void Focus(); void UnFocus(); void Interact(GameObject playerObject); } Spoiler: InteractableObject Code (CSharp): // basic component that implements IInteractable explicitly public class InteractableObject : MonoBehaviour, IInteractable { public string name; public float value; void IInteractable.Focus() { Debug.LogFormat("{0} was focused.", name); } void IInteractable.Interact(GameObject player) { Debug.LogFormat("{0} was interacted with by {1}.", name, player.name); // get the script that has the health management functions CharacterStats stats = player.GetComponent<CharacterStats>(); // if the reference is not null if(stats != null) { // use the health function stats.AddHealth(value); } } void IInteractable.UnFocus() { Debug.LogFormat("{0} was unfocused.", name); } }