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);
}
}
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: