Home > Mobile >  Unity changing from game object to ui Image
Unity changing from game object to ui Image

Time:04-04

In unity I am fetching a UI image through GameObject.Find() However when I try to change the value of another image component to the result of the game object find I get the error:


Cannot implicitly convert type 'UnityEngine.GameObject' to 'UnityEngine.UI.Image' \[Assembly-CSharp\]csharp(CS0029)

Code:


Image image;

image = GameObject.Find("the other image");

Is there a way to fetch a UI Image game object through just it's name without it being set to a generic game object?

CodePudding user response:

You are attempting to assign the value of a image to a gameObject, you simply need to get the image

Image image;

var imageGameObject = GameObject.Find("the other image");

if(imageGameObject == null)
{
    //couldn't find the gameobject 
}

image = imageGameObject.GetComponenet<Image>();

to get the actual image component

  • Related