Hi Everyone,
Firstly my coding skills are meager at best and rely on forums like this to Frankenstein code together.
My challenge now is that I want objects to move up and down using on-screen sliders. I have managed to get this working on one object and its slider, but I’m completely stumped in getting the subsequent objects moving with the remaining sliders. I show below a simplified view of the setup I need to do. Where the first slider moves the first object up and down, no problem. When I try (and do what I can think of) for the rest, I either break it completely, or the first slider would move all the objects and the rest of the sliders do nothing. I’ve watched several tutorials about sliders and movement with sliders, read all the forum slider topics I could find, but haven’t come across advice on doing this with multiple objects (with one script?)
So code for this I have at the moment looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class flyin : MonoBehaviour
{
public Slider flyinslider;
public float topposition;
public float bottomposition;
void Start()
{
flyinslider = GameObject.Find("flyinslider").GetComponent<Slider>();
flyinslider.minValue = topposition;
flyinslider.maxValue = bottomposition;
flyinslider.onValueChanged.AddListener(flyinsliderUpdate);
}
void flyinsliderUpdate(float value)
{
transform.localPosition = new Vector3(transform.position.x, value, transform.position.z);
}
}
May I kindly ask for some direction or even correction so I can assign the up and down movement of the blocks to their respective sliders?
The method you are using to retrieve the Slider searches the entire scene and returns any object named “flyinslider”. Unity - Scripting API: GameObject.Find
Take a look at the docs here, specifically the Accessing Components section and anything below it. Unity - Manual: GameObject
Depending on the setup you have, you more than likely will want to use GetComponent() instead. Remember to assign the Slider property in the editor with the one you want. Now you should have a 1:1 relationship between flyers and sliders!
Also sometimes I find it helpful to review the methods and properties of the class I’m inheriting from to see what I have at my fingertips, might be worth a read. Unity - Scripting API: Component
Lmk how it goes
Your main issue here is the use of GameObject.Find("flyinslider")
which will essentially find a random slider in the scene for you. I suggest deleting line 15 and simply dragging and dropping each slider from the hierarchy into the inspector for the cube that it should correspond to.
@oldhighscore_1 thank you for the link and reading material. I’ll have a more in-depth dive into that. Much appreciated.
@PraetorBlue , your suggestion actually worked perfectly. Happiness.
I tested with this setup and all works well now.
I place the current script below for when somebody like me, with a similar challenge, stumble onto this thread.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class flyin : MonoBehaviour
{
public Slider flyinslider;
public float uplimit;
public float bottomlimit;
void Start()
{
flyinslider.minValue = bottomlimit;
flyinslider.maxValue = uplimit;
flyinslider.onValueChanged.AddListener(flyinsliderUpdate);
}
void flyinsliderUpdate(float value)
{
transform.localPosition = new Vector3(transform.position.x, value, transform.position.z);
}
}
Simply attach this to your object, then drag the slider object from the hierarchy to the object’s script. Define the limits, and then also for the slider and done.
Much appreciate your assistance.
1 Like