- Home /
Adding a slider
So, currently my game responds to the input from a sensor. Different levels of force exerted on the sensor refer to different colours. I now want to add a slider that indicates the level of force that is being exerted on the sensor. I have added a slider but am not sure how to link it to the initial script.
This is the initial script: using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO.Ports; using System.IO; public class arduino : MonoBehaviour {
public SerialPort sp = new SerialPort("\\\\.\\COM5", 9600);// Com port and the baud rate of the arduino
Material m_Material;
GameObject Sphere;
void Awake()
{
Sphere = GameObject.FindWithTag("Player");
m_Material = GameObject.FindWithTag("Player").GetComponent<Renderer>().material;
}
void Start()
{
if (!sp.IsOpen)
{
sp.Open(); // Open
}
sp.ReadTimeout = 250; // Timeout for reading
}
// Update is called once per frame
void Update()
{
if (sp.IsOpen)
{ // Check to see if the serial port is open
try
{
//string portreading = sp.ReadLine(); // get the string output of the serial port
//float amount = int.Parse(portreading);
string value = sp.ReadTo("EOL"); //Read the information
float amount = float.Parse(value);
if ((amount > 251f))
{
m_Material.color = Color.red;
Renderer _rend = Sphere.GetComponent<Renderer>();
Sphere.GetComponent<Renderer>().material = _rend.material;
_rend.material.mainTexture = Resources.Load("face1") as Texture;
Sphere.transform.localScale = new Vector3( 2f, 0.03f*amount, 2f);
//Sphere.transform.position = new Vector3(2f, 2f , 2f);
}
else
{
m_Material.color = Color.white;
Sphere.transform.localScale = new Vector3(2f, 10f, 8f);
}
}
catch (System.Exception)
{
}
}
}
}
This is the generic slider script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Force_bar : MonoBehaviour
{
public Slider Force;
// Use this for initialization
void Start ()
{
Force.value = 300f;
}
// Update is called once per frame
void Update () {
}
}
Answer by Bravil · Oct 04, 2018 at 03:00 AM
Hi, so if I undestand it correctly you want the slider as a visual reference of the "amount" variable of your script. You can make a reference to your slider in the initial script and on your update you can do something like: yourSlider.value = amount / maxAmount; You will need to asign a maximun amount so you can give the slider a number between 0 and 1that represents the porcentage of the maximum force that your sensor can get. Hope it helps.
Answer by Benny_Whenny · Oct 04, 2018 at 10:59 PM
Is this a real life sensor or just a gameobject with a script?
Anyway provided it is a Canvas ui slider try this @Grey_Wolf9 :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SliderUpdate : MonoBehaviour {
//Reference to your slider set in inspector
public Slider MySlider;
//This is the input value from your sensor
public float SensorValue;
// Update is called once per frame
void Update () {
//Set slider's value to the sensor float
MySlider.value = SensorValue;
}
}
Thank you @Benny_Whenny. This is useful, i have a question though. How is the slider script connecting to my script that includes the data from the sensor? Like how do I link the two scripts? Ps, its a force sensor connected to an arduino
Your answer
Follow this Question
Related Questions
RGB UI Color Slider 2 Answers
Unity: Attaching a volume control slider that doesn't always exist, to another object. 0 Answers
Slider to control object rotation cause Object reference problem 2 Answers
How to put a image under a powerbar's handle thats moving? 0 Answers
Clothing Blendshapes 0 Answers