I have this error showing up, and I don´t know why. My code works until I start picking up my Axe in my game or sword. If I want to pick up something else, it´s works
I have my codes below but I don´t know why the error occurs
Using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static InteractionManager;
public class ItemObject : MonoBehaviour, IInteractable
{
public ItemData item;
public string GetInteractPrompt()
{
return string.Format("Pickup {0}", item.displayName);
}
public void OnInteract()
{
Destroy(gameObject);
Inventory.instance.addItem(item);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.InputSystem;
public class InteractionManager : MonoBehaviour
{
public float checkRate = 0.05f;
private float lastCheckTime;
public float maxCheckDistance;
public LayerMask layerMask;
private GameObject curInteractGameObject;
private IInteractable curInteractable;
public TextMeshProUGUI promptText;
private Camera cam;
void Start ()
{
cam = Camera.main;
}
void Update()
{
// true every "checkRate" seconds
if(Time.time - lastCheckTime > checkRate)
{
lastCheckTime = Time.time;
// create a ray from the center of our screen pointing in the direction we're looking
Ray ray = cam.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
RaycastHit hit;
// did we hit something?
if(Physics.Raycast(ray, out hit, maxCheckDistance, layerMask))
{
// is this not our current interactable?
// if so, set it as our current interactable
if(hit.collider.gameObject != curInteractGameObject)
{
curInteractGameObject = hit.collider.gameObject;
curInteractable = hit.collider.GetComponent<IInteractable>();
SetPromptText();
}
}
else
{
curInteractGameObject = null;
curInteractable = null;
promptText.gameObject.SetActive(false);
}
}
}
void SetPromptText ()
{
promptText.gameObject.SetActive(true);
promptText.text = string.Format("<b>[E]</b> {0}", curInteractable.GetInteractPrompt());
}
public void OnInteracInput(InputAction.CallbackContext context)
{
if(context.phase == InputActionPhase.Started && curInteractable != null)
{
curInteractable.OnInteract();
curInteractGameObject = null;
curInteractable = null;
promptText.gameObject.SetActive(false);
}
}
public interface IInteractable
{
string GetInteractPrompt();
void OnInteract();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum ItemType
{
Resource,
Equipable,
Consumable,
}
[CreateAssetMenu(fileName = "Item", menuName = "New Item")]
public class ItemData : ScriptableObject
{
[Header("Info")]
public string displayName;
public string description;
public ItemType type;
public Sprite icon;
public GameObject dropPrefab;
[Header("Stacking")]
public bool canStack;
public int maxStackAmount;
}