- Home /
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 ( https://answers.unity.com/questions/1453558/how-to-change-object-reference-in-slider-under-onv.html ) 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.
https://docs.unity3d.com/ScriptReference/Events.UnityEventBase.GetPersistentTarget.html
You are getting the error because you are trying to set the target. GetPersistentTarget can only give you the target, not change it.
Answer by Hellium · Jun 07, 2019 at 07:23 AM
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" ) ;
}
}
Brilliant! Thanks so much that solved the issue. Here's the script (for anyone who needs it) -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class $$anonymous$$usicRePop : $$anonymous$$onoBehaviour
{
Slider slider;
OptionsSwitch musicObj;
void Start()
{
slider = GetComponent<Slider>();
musicObj = FindObjectOfType<OptionsSwitch>();
if( slider != null && musicObj != null ){
slider.onValueChanged.AddListener(value => musicObj.AdjustVol(slider.value)) ;
} else {
Debug.LogError("Either the slider is not attached to the GO or no OptionsSwitch in the scene" ) ;
}
}
}
and I added this to OptionsSwitch -
AudioSource aS;
void Awake(){
aS = GetComponent<AudioSource>();
}
public void AdjustVol(float vol){
aS.volume = vol;
}
Thanks again @Hellium
Your answer
Follow this Question
Related Questions
Help, Missing Object 1 Answer
Unity: Attaching a volume control slider that doesn't always exist, to another object. 0 Answers
Moving Target Circle 2 Answers
GameObjects missing from compiled game 1 Answer
multiple targeting 1 Answer