# Need help with Toggle Swap script

**URL:** https://discussions.unity.com/t/need-help-with-toggle-swap-script/659252
**Category:** Scripting
**Tags:** Scripting
**Created:** [March 23, 2017, 2:31pm UTC](https://discussions.unity.com/t/need-help-with-toggle-swap-script/659252 "2017-03-23T14:31:22Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![eco\_bach](https://avatars.discourse-cdn.com/v4/letter/e/977dab/32.png) [@eco\_bach](https://discussions.unity.com/u/eco_bach)
#### Post date: [March 23, 2017, 2:31pm UTC](https://discussions.unity.com/t/need-help-with-toggle-swap-script/659252/1 "2017-03-23T14:31:22Z")

</div>

Hi  
Using the following class to successfully swap sprites on a Toggle object. It works but what I need is to have the Sprite swap happen OnPointerDown instead of OnValueChanged(when the mouse or finger is released).

Can anyone help?

I came across one possible solution but am having a problem implementing

> **[Best way to override OnClick with OnPointerDown for Toggle?](https://discussions.unity.com/threads/best-way-to-override-onclick-with-onpointerdown-for-toggle.390915/)**
>
> I’m trying to execute a Toggle’s OnValueChanged method using OnPointerDown instead of OnClick. Is there an elegant way to do this? Currently, I’m just using an EventTrigger with an OnPointerDown event on the toggle’s gameobject to call the method I...

```auto
using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Toggle))]
public class ToggleSpriteSwap : MonoBehaviour
{
    [SerializeField]
    private Image targetImage;e]

    [SerializeField]
    private Sprite onSprite;
    [SerializeField]
    private Sprite offSprite;
    [SerializeField]
    private bool isOn;

    public bool IsOn { get { return isOn; } set { isOn = value; UpdateValue(); } }
    //public event Action<bool> onValueChanged;

    private Toggle toggle;

    // to set initial value and skip onValueChanged notification
    public void Initialize(bool value)
    {
        isOn = value;
        UpdateValue(false);
    }

    // Use this for initialization
    void Start()
    {
        toggle = GetComponent<PointerDownToggle>();
        toggle.transition = Selectable.Transition.None;
         toggle.onValueChanged.AddListener((t) => { OnClick(); });
        
    }

    void OnClick()
    {
        //isOn = !isOn;
        isOn=toggle.isOn;
        UpdateValue();
    }

    private void UpdateValue(bool notifySubscribers = true)
    {
       // if (notifySubscribers && onValueChanged != null)
           // onValueChanged(isOn);

        if (targetImage == null)
            return;

        targetImage.sprite = isOn ? onSprite : offSprite;
    }
}

```

---

<div class="post-metadata">

### Author: ![eco\_bach](https://avatars.discourse-cdn.com/v4/letter/e/977dab/32.png) [@eco\_bach](https://discussions.unity.com/u/eco_bach)
#### Post date: [March 23, 2017, 3:28pm UTC](https://discussions.unity.com/t/need-help-with-toggle-swap-script/659252/2 "2017-03-23T15:28:13Z")

</div>

Ok so almost there. Everything works except my Toggle group isn’t updated. ie members of the group NOT pressed don’t switch to their ‘OFF’ state. I added the 'group.NotifyToggleOn(this) on line 12 and also tried 'base.group.NotifyToggleOn(this); No difference!

Extending Toggle using the following

```auto
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PointerDownToggle : Toggle {
 public override void OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData)
    {
    }
    public override void OnPointerDown (UnityEngine.EventSystems.PointerEventData eventData)
    {
        base.OnPointerClick (eventData);
        group.NotifyToggleOn(this);
    }
}

```

and then my revised sprite swap class is

```auto
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

[RequireComponent(typeof(Toggle))]
public class ToggleSpriteSwap : MonoBehaviour, IPointerDownHandler
{
    [SerializeField]
    private Image targetImage;

    [SerializeField]
    private Sprite onSprite;
    [SerializeField]
    private Sprite offSprite;
    [SerializeField]
    private bool isOn;

    public bool IsOn { get { return isOn; } set { isOn = value; UpdateValue(); } }
 
    private PointerDownToggle toggle;

    public void OnPointerDown(PointerEventData data)
    {
        isOn=toggle.isOn;
        UpdateValue();
        Debug.LogWarning ("1 ASSERT OnPointerDown toggle ==" + toggle);
        Debug.LogWarning ("2 ASSERT OnPointerDown toggle.isOn ==" + toggle.isOn);
    }

    // to set initial value and skip onValueChanged notification
    public void Initialize(bool value)
    {
        isOn = value;
        UpdateValue(false);
    }

    // Use this for initialization
    void Start()
    {
        toggle = GetComponent<PointerDownToggle>();
        toggle.transition = Selectable.Transition.None;
        Debug.LogWarning ("0 ASSERT Start toggle ==" + toggle);
     }
    private void UpdateValue(bool notifySubscribers = true)
    {
        if (targetImage == null)
            return;

        targetImage.sprite = isOn ? onSprite : offSprite;
    }
}

```

---

<div class="post-metadata">

### Author: ![methos5k](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/methos5k/32/387158_2.png) [@methos5k](https://discussions.unity.com/u/methos5k)
#### Post date: [April 5, 2017, 6:10pm UTC](https://discussions.unity.com/t/need-help-with-toggle-swap-script/659252/3 "2017-04-05T18:10:08Z")

</div>

Did you make sure it added your custom Toggle type instead of the regular one ?
