How to change onValueChanged target GameObject in a Slider during runtime?

I have two canvases, one is used for my Main Menu Scene and one is used to display scores in each level.
When I return to the main menu to adjust the volume, my sliders in my MainMenu/Options no longer access the Music GameObject even though the Music gameObject is set to ‘DoNotDestroy’ and works fine during the transition to the first level.


I am therefore trying to repopulate the gameObject accessed by the slider (script below).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MusicRePop : MonoBehaviour
{
  Slider slider;
  OptionsSwitch musicObj;

  void Start()
  {
    slider = GetComponent<Slider>();
    musicObj = FindObjectOfType<OptionsSwitch>();
  }

  void Update()
  {
    // Debug.Log(slider.onValueChanged.GetPersistentTarget(0));
    if(slider.onValueChanged.GetPersistentTarget(0) == null){
      slider.onValueChanged.GetPersistentTarget(0) = musicObj;
    } else {
      Debug.Log(slider.onValueChanged.GetPersistentTarget(0));
    }
  }
}

I have searched google and unity documentation (https://docs.unity3d.com/ScriptReference/UI.Slider.SliderEvent.html) and looked at the only reference to the subject that I could find ( How to change object reference in slider under OnValueChanged during runtime with c# script? Not through editor - Questions & Answers - Unity Discussions ) but this question does not provide a clear answer. Where:-

Brogan69 states this - “You don’t need to do that in code.”

When I run the above code I get the following error…
Assets\Scripts\MusicRePop.cs(21,7): error CS0131: The left-hand side of an assignment must be a variable, property or indexer

Can any body point me in the right direction? Thanks.

Remove the object in the onValueChanged event in the inspector and add the listener by code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 public class MusicRePop : MonoBehaviour
 {
   Slider slider;
   OptionsSwitch musicObj;
   void Start()
   {
     slider = GetComponent<Slider>();
     musicObj = FindObjectOfType<OptionsSwitch>();
     if( slider != null && musicObj != null )
         slider.onValueChanged.AddListener( value => musicObj.FunctionToCall( arguments ) ) ;
     else
         Debug.LogError("Either the slider is not attached to the GO or no OptionsSwitch in the scene" ) ;
   }
 }