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.
I’m not sure what the simplest method to try to share with you is when you’re so new.
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…
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 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*.