- Home /
Scrollbar to move left and right
Okay so I am trying to make it so that you can move the character left and right with the new UI scrollbar. I am using this script.
public float moveSpeed;
private float moveVelocity;
public GameObject scrollbar;
void Update () {
moveVelocity = 0f;
if (scrollbar >= 0.5)
{
moveVelocity = moveSpeed;
}
if(scrollbar <= 0.5)
{
moveVelocity = -moveSpeed;
}
}
So the problem is that I keep getting this error,
Assets/Move.cs(27,21): error CS0019: Operator >' cannot be applied to operands of type
UnityEngine.GameObject' and `double'
http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-scrollbar
Get the bar as a ScrollBar, not a GameObject. Read its .value field. Optionally, set the value back to 0.5f when the user lets go, or use the value's distance from 0.5f to influence the speed of movement.
Also, most things in Unity want floats, not doubles. Qualify your "mixed numbers" with an f unless you actually want a double.
float foo = 1.5f;
double bar = 1.5;
Answer by spiceboy9994 · Mar 21, 2015 at 12:02 AM
Assuming that your public scrollbar object indeed has an UI.Scrollbar component attached, you should try this
public GameObject scrollbar;
//Add a reference to the scrollbar component
Scrollbar myScrollbar;
void Start() {
myScrollbar = scrollbar.GetComponent<Scrollbar>();
}
void Update () {
moveVelocity = 0f;
if (myScrollbar.value >= 0.5)
{
moveVelocity = moveSpeed;
}
if(scrollbar.value <= 0.5)
{
moveVelocity = -moveSpeed;
}
}
Don't forget to add the reference to the UnityEngine.UI namespace at the beginning of your class so you can have access to the objects. Just go to the top of your class and add:
using UnityEngine.UI;
Regards
If that worked... do you $$anonymous$$d a thumbs up vote? :D?
Your answer
![](https://koobas.hobune.stream/wayback/20220613190650im_/https://answers.unity.com/themes/thub/images/avi.jpg)