moving object with slider

hello,

I have a problem with an older script I used with unity. With this i was able to move an object using the prefab slider. Now the object moves randomly and uses all three axes instead of the one in the code. Can someone tell me what I’m doing wrong?

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ViewModel : MonoBehaviour
{
    public Text buttonText;
    public GameObject koppeling_links;
    public Slider slider;

    public void Slider_Changed(float newValue)
    {
        Vector3 pos = koppeling_links.transform.position;
        pos.x = (-1 * newValue)-10;
        koppeling_links.transform.position = pos;

       
    }




    public void Button_Click()
    {
        Debug.Log("Hello, World!");
    }

    public void Button_String(string msg)
    {
        buttonText.text = msg;
    }

    public void Toggle_Changed(bool newValue)
    {
        koppeling_links.SetActive(newValue);
        slider.interactable = newValue;
    }

    public void Text_Changed(string newText)
    {
        float temp = float.Parse(newText);
        koppeling_links.transform.localScale = new Vector3(temp, temp, temp);
    }

}

Slider_Changed & Text_Changed methods are the ones to look at.
They do exactly what they were told to do - resize and move. :slight_smile:

Thanks for the reply! these were indeed unnecessary in this code. I’ve set it to the following but still get the same problem:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ViewModel : MonoBehaviour
{

    public GameObject koppeling_links;
    public GameObject koppeling_rechts;
    public Slider slider;

    public void Slider_Changed(float newValue)
    {
        Vector3 pos = koppeling_links.transform.position;
        pos.x = (-1 * newValue)-10;
        koppeling_links.transform.position = pos;

       
    }




    public void Button_Click()
    {
        Debug.Log("Hello, World!");
    }



}

I assume you are assigning the method Slider_Changed on the value change trigger of the Slider, in the inspector.
As it only accepts a fixed value as parameter, as far as I know, it will result on your newValue to always have that fixed value, hence the reason why you see it go to a “random” place, which is actually the value in there.

To fix the issue you can do the following changes:

    public void Slider_Changed(float newValue)
    {
        Vector3 pos = koppeling_links.transform.position;
        pos.x = (-1 * slider.value) - 10;
        koppeling_links.transform.position = pos;
    }

That was it! in the start it was still floating on the y axis but by changing the scene and slider that was also fixed. This is the final code:

    public void Slider_Changed(float newValue)
    {
        Vector3 pos = koppeling_links.transform.position;
        pos.x = (-1 * slider.value);
        koppeling_links.transform.position = pos;
    }
1 Like