Answer the question
In order to leave comments, you need to log in
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;
}
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
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question