Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ToroidGames · Jun 07, 2019 at 12:55 AM · gameobjectslidertargetmissingpersistent

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. alt text 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.

onvaluechanged.png (47.1 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MadDevil · Jun 07, 2019 at 03:40 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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" ) ;
    }
  }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ToroidGames · Jun 07, 2019 at 01:02 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

160 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges