Silder Question

I am making a game where a slider changes the properties of my player. It does this by sending the slider value to a data handler gameobject first, and then my player accesses the value from the data handler.

Due to issues with preserving the data handler through scenes, I have moved the loading of my data handler to another scene. My previously working game has now stopped working due to the slider no longer having a On Value Changed object reference (and therefore a function reference), as it was previously found via a drag and drop in the unity interface.

I would like to know how to make the slider find my data handler which has been carried over from another scene. Do I need a c# script to do this? or is there another way to make the slider find the reference?

If you know how to add the listener in code, that’s 1 step done.

You can find the data handler with FindObjectOfType (provided there’s just 1 of them).
You could also set an instance variable (static) on the data handler, which the slider could then look for.
Both of those would require a script.

Does that make sense? If you need more information, feel free to ask…

Thanks for your reply. There is actually 3 sliders, which correspond to 3 different values.

Im a beginner at coding and I generally dont understand programming talk, What you said seems pretty complicated to me. I had it working, I just want the slider to find the DataHandler gameobject without a drag and drop, and call the function. So I assume I need to add a new script to the slider, and have something along the lines of On Value Change () {GameObject.Find(“DataHandler”)().float. I just dont know how to structure it.

3339595--260679--Untitled.png

I’m not sure what the simplest method to try to share with you is when you’re so new. :slight_smile:
If you have 1 DataHandlerScript, you can try FindObjectOfType, which is at least better than GameObject.Find.
So, the assumption you made could adapt to that.

If it used to send it to a float / method, you can do the same thing…

I mean

On Value Change () {GameObject.Find(“DataHandler”)(). Function ()

public void OnValueChanged(float value) {
   FindObjectOfType<DataHandlerScript>().YourFunction(value);
  }

Try that…

Is OnValueChanged () the right method? I just guessed.

Well, technically you can call it anything, but it requires the parameter (float whatever)
I mean you could call it : public void IJustDidAnUpdate(float val) etc
:slight_smile: It’s just a name. heh. But you do have to reference it in the OnValueChanged of the Slider in the inspector.

To be a bit more thorough, there is technically “no method” (until you assign one). The delegate/event is there, and you (can)choose to add a function (or more than 1) to it, to be called by unity when the slider’s value changes*.

1 Like

Right, Got it. It wasnt as hard as i was making it out to be. Just one question. Thanks!

Cool.

Remember, as you go forward over the days/weeks… GameObject.Find and such are not the greatest options. :slight_smile: lol You’ll learn better ways.

However, for now, I’m glad that you have it working. Enjoy your game!

1 Like