K
K
KeysCG2022-04-10 23:07:35
Unity
KeysCG, 2022-04-10 23:07:35

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

Here is the whole code:
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;
    }
}

Thanks in advance for your help :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GFX Data, 2022-04-10
@KeysCG

GetComponent s InChildren()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question