- Home /
Add a value to a string
Hi, I've created a code that gets the localscale value and transforms it into a string then changes its unit of measurement to centimeters by multiplying it to 10. Problem is, it doesnt seem to work, are there any ways to add/subtract/multiply the localscale value that I've converted into string? Here's my vanilla code without formulas.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScaleSlider : MonoBehaviour {
public Slider sliderLength;
public Slider sliderWidth;
public Slider sliderHeight;
public Text textLength;
public Text textWidth;
public Text textHeight;
// Use this for initialization
void Start () {
sliderWidth.value = 1f;
sliderLength.value = 1f;
sliderHeight.value = 1f;
}
void Update () {
textLength.text = transform.localScale.x.ToString();
textWidth.text = transform.localScale.y.ToString();
textHeight.text = transform.localScale.z.ToString();
transform.localScale = new Vector3(sliderLength.value*1, sliderWidth.value*1, sliderHeight.value*1);
}
}
At line 22-24. I removed the code since it didnt work out and was wondering if there's any other way.
Wait, so you asked a question about some of your code doesn't work, but you removed that code from the question? So how should we tell what you did wrong?
If i understood you correctly you want to multiply the localscale values with 10.
You are multiplying with 1 and not 10.
You are not multiplying the localscale value, but a sliderLength.value, which initially is set to 1 and additionally is a string.
Why dont you use a float type for those variables (textLength etc.) and when you need them as a string you just could use ToString() as you already did with the localScale?
Answer by Bunny83 · Nov 22, 2016 at 11:51 AM
Since you didn't include your problematic code in your question we can only guess. I think you want something like this:
void Update ()
{
transform.localScale = new Vector3(sliderLength.value*1, sliderWidth.value*1, sliderHeight.value*1);
Vector3 scale = transform.localScale * 10;
textLength.text = scale.x.ToString();
textWidth.text = scale.y.ToString();
textHeight.text = scale.z.ToString();
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Does unity C# support an exec method? 1 Answer
How to Instantiate from correct Icon. 1 Answer
Converting a string to an int 2 Answers