- Home /
Cannot convert type 'UnityEngine.GameObject' to 'GameObject'
Hello all, I keep getting the error: error CS0030: Cannot convert type 'UnityEngine.GameObject' to 'GameObject'. Due to that everything below is the loop is also giving errors, but they should be fixed after i fix the loop. My code is provided below. Im also pretty new to foreach loop, maybe im messing up there? Thanks in advance.
// iterates through pooledObstacles and return the pooledObstacle if it is not active in hierarchy
foreach (GameObject i in pooledObstacles)
{
if(!i.activeInHierarchy)
{
i.SetActive(true);
return i;
}
}
Answer by RobAnthem · Jun 03, 2018 at 03:12 PM
There is likely another class or enum named GameObject, so it is creating a conflict. Best practice is to avoid making anything with the same name as something else unless it is in another Namespace. Easiest solution would be...
foreach (UnityEngine.GameObject i in pooledObstacles)
{
if(!i.activeInHierarchy)
{
i.SetActive(true);
return i;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612164005im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
C# script editor won't open? What should I do 2 Answers
Draw call minimizer 1 Answer
Help with script 1 Answer
[C#] Quaternion Rotations with Input.GetAxis problems. 1 Answer