# Changing Button Image On Click (Javascript)

**URL:** <https://discussions.unity.com/t/changing-button-image-on-click-javascript/659462>\
**Category:** UGUI\
**Tags:** UGUI\
**Created:** [March 25, 2017, 5:23pm UTC](https://discussions.unity.com/t/changing-button-image-on-click-javascript/659462 "2017-03-25T17:23:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fireking883](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/fireking883/32/313615_2.png) [@Fireking883](https://discussions.unity.com/u/Fireking883)\
**Post date:** [March 25, 2017, 5:23pm UTC](https://discussions.unity.com/t/changing-button-image-on-click-javascript/659462/1 "2017-03-25T17:23:22Z")

</div>

Exactly what the title says. I have tried looking it up but nothing seems to work. The last thing I tried was:

```javascript
     if(BID == 1){
         B1.GetComponent.<Image>() = CubeButton;

```

Which gives the error: Assets/Inventory.js(96,41): BCE0049: Expression ‘self.B1.GetComponentof UnityEngine.UI.Image’ cannot be assigned to.

I am just trying to change the button image to an image of a cube when I click on it.

---

<div class="post-metadata">

**Author:** ![mikael\_juhala](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/mikael_juhala/32/288560_2.png) [@mikael\_juhala](https://discussions.unity.com/u/mikael_juhala)\
**Post date:** [March 25, 2017, 7:33pm UTC](https://discussions.unity.com/t/changing-button-image-on-click-javascript/659462/2 "2017-03-25T19:33:02Z")

</div>

You’re trying to set the instance of component, which won’t work. You should change its sprite with _Image.sprite_.  
[https://docs.unity3d.com/ScriptReference/UI.Image-sprite.html](https://docs.unity3d.com/ScriptReference/UI.Image-sprite.html)
