tutorial: Unity3D : how to control scale with slider
Posted October 16th, 2015 by Warren BlythSo here’s a quick way to scale a GameObject with a slider (in C#)
basically:
you need to make a script that scales the object based on current value of slider. Something like:
public void slideScale(float t){
transform.localScale = new Vector3(t, t, t);
}
Puit it on a GameObject. Then tell the slider UI object to call this script (by clicking around in its Inspector, under “On Value Changed(single)”. boom.
Long Version
1) I made a slider childed to my canvas (right click your canvas in the Hierarchy and choose “UI > Slider” .
2) I wrote a script that this slider would send it’s value into.
Right click in the Project window, and choose “create > c# script”. When it is first created you are prompted to name it (- this will set up the name of some automatically generated lines inside of it! so if you missed your chance to name it initially, just delete it and create another). I named my script “slideFluidAmount”.
I double click it to open in your text editor.
In this example I renamed “update” to a new unique function name, “slideScale” (you don’t have to have update() or start(), they are just automatically generated to save you time.). Nothing will access slideScale unless I tell it to. (see step 4).
I added a float variable in the incoming arguments called “textUpdateNumber”. This will hold the value of the slider as it is being dragged around. It will be the same throughout the function, but this function will be called many times per second with new values (as long as the user drags).
All i need is to do is change the local scale to the incoming value. (by default sliders are set to range from 0 to 1, which happens to be the usual scale amount you’d want to mess with).
so i access the scale of whatever GameObject has this script by saying “transform.localScale =” and then I need to give it x,y,z values. so i create a vector3 on the spot, and slip in my textUpdateNumber for the y value. I leave x and z at 1, which is the same as saying I leave those two things at their expected default scale.
extra ramble:
For this game, i wanted the user to drag the slider to 80% of it’s length, and then make some stuff disappear in the scene.
I’ve already set up a mesh inside my test tubes with it’s origin at the bottom most point – so as it is scaled, the test tube will appear to fill up.
so i added an if statement to check for a range of values
…(more on this later)
3) I drag my script from the Project window ONTO a gameobject. I drag it to a gameobject in the hierarchy, OR i can click a gameobject and drag the script into it’s Inspector. eitherway, this script ends up being just another component on a GameObject.
4) I open the Slider’s Inspector panel, and scroll down to the bottom. Under
“On Value Changed(single)” i click the “+”. I click the small circle dot “(.)” under Runtime Only and select my GameObject (from the step above. It needs to be a gameobject that has my script on it). Now that the inspector knows the gameobject I’m going to send numbers to, it gives me a pull down to the right with all the things that can accept numbers. I need to navigate through this pulldown until i find the “slideScale” name. every unique function name I’ve made in my script will show up in this list.
5) that’s it. now when you run the game, any slider change will be sent to the script on this gameobject.
October 12th, 2016 at 10:46 am
i tried ur code and it says
error CS0246: The type or namespace name `changeMyColor’ could not be found. Are you missing a using directive or an assembly reference?
how to fix
October 17th, 2016 at 11:31 pm
Looking at my screenshot, “changeMyColor” only seems to appear on a line that is commented out.
Hopefully you can just delete that from whatever you wrote?
December 4th, 2016 at 2:10 am
I’ve been trying to get this working for weeks. No tutorials worked I tried so many different ways to get it working. Thank you.