Search Unity

Question about scripting button image changes

Discussion in 'Scripting' started by Sylvir, Aug 28, 2015.

  1. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    hey everyone,

    I am attempting to change the button image to a new image when something is clicked on. The code I have right now is here..

    Code (CSharp):
    1. public Toggle balsaLogToggle;
    2.     public Toggle appleLogToggle;
    3.     public Toggle ashLogToggle;
    4.  
    5.     public Button campFireButton;
    6.    
    7.     public Image campFireOn;
    8.     public Image campFireOff;
    9.  
    10.     // Use this for initialization
    11.     void Start ()
    12.     {
    13.         campFireButton = gameObject.Find ("Camp Fire Button").GetComponent<Button> ();
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update ()
    18.     {
    19.    
    20.     }
    21.  
    22.     void OnClickWhatLogToBurn()
    23.     {
    24.         if(balsaLogToggle.isOn)
    25.         {
    26.             GameController.control.balsaLogs -= 1;
    27.             campFireButton = campFireOn;  
    28.         }
    29.         else if(appleLogToggle.isOn)
    30.         {
    31.             GameController.control.appleLogs -= 1;
    32.             campFireButton = campFireOn;
    33.         }
    34.         else if(ashLogToggle.isOn)
    35.         {
    36.             GameController.control.ashLogs -= 1;
    37.             campFireButton = campFireOn;
    38.         }
    39.        
    40.     }
    41.  
    42.  
    43. }
    I am receiving this error message
    Assets/LightLogScript.cs(33,25): error CS0029: Cannot implicitly convert type `UnityEngine.UI.Image' to `UnityEngine.UI.Button'

    my question is what should i be using instead of the public Image to work with the button images? Am I just thinking through the logic on this the wrong way, or would this idea work if I was using the correct type instead of UnityEngine.UI.Image ? thanks!
     
  2. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    would this be a better path to get this resault? campFireButton.GetComponent<Button> ().targetGraphic = campFireOn;
     
  3. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    Solved. My Apologies for posting too soon... got more ideas once i posted this..

    public Sprite campFireOn;
    public Sprite campFireOff;

    // Use this for initialization
    void Start ()
    {

    }

    // Update is called once per frame
    void Update ()
    {

    }

    public void OnClickWhatLogToBurn()
    {
    if(balsaLogToggle.isOn)
    {
    GameController.control.balsaLogs -= 1;
    campFireButton.GetComponent<Image> ().sprite = campFireOn;
    }

    this is how i solved it in case anyone has a similar issue thanks