D
D
Diana Stoyka2020-12-30 16:25:45
C++ / C#
Diana Stoyka, 2020-12-30 16:25:45

I have a problem with foreach?

I am new to c#

if (this.CompareTag("Cube") && other.CompareTag("Cube"))
        {
            foreach(Activator button in FindObjectOfType<Activator>()) // тут пишет ошибку
            {
                button.canPuch = false;
            }

mistake
error CS1579: foreach statement cannot operate on variables of type 'Activator' because 'Activator' does not contain a public instance definition for 'GetEnumerator'


public class Activator : MonoBehaviour
{
    public GameObject[] firstGroup;
    public GameObject[] secondGroup;
    public Activator button;
    public Material normal;
    public Material transparent;
    public bool canPuch;

    private void OnTriggerEnter(Collider other)
    {
        if (canPuch)
        {
            if (other.CompareTag("Cube") || other.CompareTag("Player"))
            {
                foreach (GameObject first in firstGroup)
                {
                    first.GetComponent<Renderer>().material = normal;
                    first.GetComponent<Collider>().isTrigger = false;
                }
                foreach (GameObject second in secondGroup)
                {
                    second.GetComponent<Renderer>().material = transparent;
                    second.GetComponent<Collider>().isTrigger = true;
                }
                GetComponent<Renderer>().material = transparent;
                button.GetComponent<Renderer>().material = normal;
                button.canPuch = true;
            }
        }
    }

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2020-12-30
@Dianka05

FindObjectOfType<Activator> returns a single instance, so you don't need a loop in your case.
If you want to get all instances of the Activator type, then you need to use FindObjectsOfType - then get a list that you can walk through with foreach

S
SunnyWolf, 2020-12-30
@NEDOprogrammer

The foreach statement cannot operate on variables of type Activator because Activator does not contain a public instance definition for GetEnumerator

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question