Hey all, I am trying to change a button image in c# and have the following error: Assets/Scripts/SpawnTiles.cs(41,23): error CS1061: Type `UnityEngine.GameObject' does not contain a definition for `image' and no extension method `image' of type `UnityEngine.GameObject' could be found. Are you missing an assembly reference? The source is: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class SpawnTiles : MonoBehaviour { private GameObject[] STiles = new GameObject[8]; private GameObject[] OTiles = new GameObject[4]; public GameObject Tiles; public float Xpos = -300.00f; public float Xincrement = 60.0f; private GameObject UiPanel; private string[] MathOperators = { "+", "-","X", "÷" }; private int TileNum; private GameObject TextField; private int[] NumList = new int[8]; private Image GameObjectPNG; private Image ButtonImage; // Use this for initialization public void Start () { UiPanel = GameObject.Find("Panel"); for(int i = 0;i < NumList.Length;i++) { NumList[i] = UnityEngine.Random.Range(1, 9); } TileSpawn(); } public void TileSpawn() { for (int i = 0;i < STiles.Length;i++) { // STiles[i] = Instantiate(Resources.Load("Button")) as GameObject; STiles[i] = Instantiate(Tiles); if(i % 2 == 1) GameObjectPNG = Resources.Load<Image>("eight"); else GameObjectPNG = Resources.Load<Image>("six"); STiles[i].image = GameObjectPNG; STiles[i].transform.position = new Vector3(Xpos, -300.00f,-27f); STiles[i].transform.SetParent(UiPanel.transform,false) ; STiles[i].name = NumList[i].ToString(); Xpos += Xincrement; //test } } } Any help would be greatly appreciated.
Here you are trying to assign an image to a gameoboject Code (CSharp): STiles[i].image = GameObjectPNG; An image would be a component on a gameobject.
No, STiles is still an array. You would need to get the image component within the gameobject though using GetComponent. So; STiles[ i ].GetComponent<Image>(); Think of a gameobject as an empty box. You can't set it as an image because it isn't one, however, it can contain and image which is what you wish to alter.