Guys i made a script for interaction and examine system but when i examine items ,the event that i want to happen is repeating, for example 7 times, if i press the f button 7 times (sry i dont know how to explain it)
here is the Interaction system script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InteractionSystem : MonoBehaviour
{
[Header("Detection Fields")]
public Transform detectionPoint;
private const float detectionRadius = 0.5f;
public LayerMask detectionLayer;
public GameObject detectedObject;
[Header("Examin Fields")]
public GameObject examineWindow;
public Image examineImage;
public Text examineText;
public bool isExaming;
[Header("Others")]
public List<GameObject> pickedItems=new List<GameObject>();
void Update()
{
if(DetectObject())
{
if(InteractInput())
{
detectedObject.GetComponent<Item>().Interact();
}
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.green;
Gizmos.DrawSphere(detectionPoint.position, detectionRadius);
}
bool InteractInput()
{
return Input.GetKeyDown(KeyCode.F);
}
bool DetectObject()
{
Collider2D obj = Physics2D.OverlapCircle(detectionPoint.position,detectionRadius,detectionLayer);
if(obj == null)
{
detectedObject = null;
return false;
}
else
{
detectedObject = obj.gameObject;
return true;
}
}
public void PickUpItem(GameObject item)
{
pickedItems.Add(item);
}
public void ExamineItem(Item item)
{
if(isExaming)
{
Debug.Log("Close");
examineWindow.SetActive(false);
isExaming = false;
}
else
{
Debug.Log("Examine");
examineImage.sprite = item.GetComponent<SpriteRenderer>().sprite;
examineText.text = item.descriptionText;
examineWindow.SetActive(true);
isExaming = true;
}
}
}
and this is the items script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
[RequireComponent(typeof(BoxCollider2D))]
public class Item : MonoBehaviour
{
public enum InteractionType { NONE, PickUp, Examine }
public InteractionType type;
[Header("Examine")]
public string descriptionText;
public UnityEvent customEvent;
private void Reset()
{
GetComponent<Collider2D>().isTrigger = true;
gameObject.layer = 10;
}
public void Interact()
{
switch(type)
{
case InteractionType.PickUp:
FindObjectOfType<InteractionSystem>().PickUpItem(gameObject);
gameObject.SetActive(false);
break;
case InteractionType.Examine:
FindObjectOfType<InteractionSystem>().ExamineItem(this);
break;
default:
Debug.Log("NULL ITEM");
break;
}
customEvent.Invoke();
}
}