Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Grey_Wolf9 · Oct 03, 2018 at 11:02 PM · unity 5slider

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 () {
         
     }
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Grey_Wolf9 · Oct 04, 2018 at 06:17 PM 0
Share

Thank you for your reply @Bravil. II will try that

avatar image
0

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;
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Grey_Wolf9 · Oct 11, 2018 at 09:56 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

183 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges