0

I am trying to use GameObject.FindObjectsOfTypeAll() in UnityEngine and it is asking for an Il2Cpp class, and if i do GameObject.FindObjectsOfTypeAll(typeof(Image)) i get the error cannot convert from "System.Type" to "Il2cppSystem.Type"

I also tried using GameObject.FindObjectsOfTypeAll(Il2CppSystem.Type.GetType("Image")) But this ends in a null reference exception.

This is what Im trying to do but it doesn't enter the foreach obviously

foreach (GameObject image in GameObject.FindObjectsOfTypeAll(typeof(Image)))
            {
                image.GetComponent<Image>().color = panelColor;
            }

and

foreach (GameObject image in GameObject.FindObjectsOfTypeAll(Il2CppSystem.Type.GetType("Image")))
            {
                image.GetComponent<Image>().color = panelColor;
            }
CatShark
  • 1
  • 3
  • You might want to explain what a "GameObject.FindObjectsOfTypeAll()" is. Specifically, where does GameObject come from? What library is it? Perhaps, by adding those keywords in your question, you'll get attention from someone who's worked with that library. – Andrew Rondeau Sep 27 '21 at 00:43
  • @AndrewRondeau the question is tagged [`Unity3d`](https://stackoverflow.com/tags/unity3d/info) .. everyone a little bit familiar with it will know that [`GameObject`](https://docs.unity3d.com/ScriptReference/GameObject.html) is one of the most basic types in Unity and [`Object.FindObjectsOfType`](https://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html) is usually rather used directly via `UnityEngine.Object` ;) – derHugo Sep 27 '21 at 04:25
  • Actually ses pretty odd since for the last 5 years **if** I head to deal with the typed version I always used `System.Type` ... in general in your case I would prefer to use the generic version `Object.FindObjectsOfType()` though – derHugo Sep 27 '21 at 04:27

1 Answers1

0
Resources.FindObjectsOfTypeAll(typeof(UnityEngine.UI.Image)

or

 Resources.FindObjectsOfTypeAll<UnityEngine.UI.Image>()

maybe you using some namespace it also have "image" class. So try put it full namespace of type .

TimChang
  • 2,249
  • 13
  • 25
  • That is using the generic version which is something completely different than using the version taking a `Type` parameter ;) In most cases it is preferred to use the generic if the type is not dynamic anyway though which seems to be the case here – derHugo Sep 27 '21 at 04:25
  • yes generic is better . it's not get type at runtime. – TimChang Sep 27 '21 at 06:37