Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Assigning an image to a button

Discussion in 'Scripting' started by AscendDev, Apr 28, 2017.

  1. AscendDev

    AscendDev

    Joined:
    Apr 28, 2017
    Posts:
    51
    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):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class SpawnTiles : MonoBehaviour {
    7.     private GameObject[] STiles = new GameObject[8];
    8.     private GameObject[] OTiles = new GameObject[4];
    9.     public GameObject Tiles;
    10.     public float Xpos = -300.00f;
    11.     public float Xincrement = 60.0f;
    12.     private GameObject UiPanel;
    13.     private string[] MathOperators = { "+", "-","X", "÷" };
    14.     private int TileNum;
    15.     private GameObject TextField;
    16.     private int[] NumList = new int[8];
    17.     private Image GameObjectPNG;
    18.     private Image ButtonImage;
    19.  
    20.     // Use this for initialization
    21.     public void Start () {
    22.         UiPanel = GameObject.Find("Panel");
    23.         for(int i = 0;i < NumList.Length;i++)
    24.         {
    25.             NumList[i] = UnityEngine.Random.Range(1, 9);
    26.         }
    27.         TileSpawn();
    28.     }
    29.    
    30.     public void  TileSpawn()
    31.     {
    32.        
    33.      
    34.         for (int i = 0;i < STiles.Length;i++) {
    35.             //         STiles[i] = Instantiate(Resources.Load("Button")) as GameObject;
    36.             STiles[i] = Instantiate(Tiles);
    37.             if(i % 2 == 1)
    38.                 GameObjectPNG = Resources.Load<Image>("eight");
    39.             else
    40.                 GameObjectPNG = Resources.Load<Image>("six");
    41.             STiles[i].image = GameObjectPNG;
    42.             STiles[i].transform.position = new Vector3(Xpos, -300.00f,-27f);
    43.             STiles[i].transform.SetParent(UiPanel.transform,false) ;
    44.             STiles[i].name = NumList[i].ToString();
    45.  
    46.             Xpos += Xincrement;
    47.             //test
    48.         }
    49.  
    50.     }
    51. }
    52.  
    Any help would be greatly appreciated.
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Here you are trying to assign an image to a gameoboject

    Code (CSharp):
    1. STiles[i].image = GameObjectPNG;
    An image would be a component on a gameobject.
     
  3. AscendDev

    AscendDev

    Joined:
    Apr 28, 2017
    Posts:
    51
    So the correct code should be STiles.GetCompoment???
     
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    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.
     
    Last edited: Apr 28, 2017
    AscendDev likes this.
  5. AscendDev

    AscendDev

    Joined:
    Apr 28, 2017
    Posts:
    51
    Got it! Thanks very much.