Answer the question
In order to leave comments, you need to log in
How can foreach be corrected in this case?
Good day! I'm trying to make it so that in the inventory it would be impossible to put an object in a cell if it is already occupied, but foreach swears that (The foreach operator does not work with variables of type "CanvasGroup", since "CanvasGroup" does not contain a public definition of an instance or extension for "GetEnumerator"), how can I fix this?
The problem is in this piece of code:
foreach(CanvasGroup canvasGroup in canvasInventory.GetComponentInChildren<CanvasGroup>())
{
canvasGroup.blocksRaycasts = false;
}
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ItemDrag : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
RectTransform rectTransform;
Canvas canvasInventory;
Button itemButton;
void Start()
{
rectTransform = GetComponent<RectTransform>();
itemButton = GetComponent<Button>();
canvasInventory = GetComponentInParent<Canvas>();
}
public void OnBeginDrag(PointerEventData eventData)
{
rectTransform.SetParent(canvasInventory.GetComponent<RectTransform>());
GetComponent<CanvasGroup>().blocksRaycasts = false;
itemButton.enabled = false;
foreach(CanvasGroup canvasGroup in canvasInventory.GetComponentInChildren<CanvasGroup>())
{
canvasGroup.blocksRaycasts = false;
}
}
public void OnDrag(PointerEventData eventData)
{
rectTransform.anchoredPosition += eventData.delta / canvasInventory.scaleFactor;
}
public void OnEndDrag(PointerEventData eventData)
{
GetComponent<CanvasGroup>().blocksRaycasts = true;
itemButton.enabled = true;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question