- Home /
How to change a float value with UI buttons,How Can I control a character with UI buttons?
I want to change the value to -1 if the UI button is pressed and holded down and +1 if another button is pressed and holded down, then, in another script(character controller) i want to get the total of both( either -1, +1, or 0 if none(or both) of those 2 buttons are getting pressed). Thanks.
All this is for simulating a "GetAxisRaw" with two UI buttons, if you know how to do this in other way.,I'm trying to change the value of one float to -1 if one button is pressed and holded down, to 1 if another button is pressed and holded down and 0 if none of the buttons are getting pressed. The Main script to control the character is in the character, so I don't know how to interact between the two scripts
Answer by UDN_9a915d40-27e1-405b-b1cc-83be8be3e71d · Dec 28, 2017 at 04:00 PM
Let's say they are on the same gameObject. The first script is called Foo.cs
and the second is called Bar.cs
. To access the scripts if they are on the same object you can simply GetComponent<Foo>();
or GetComponent<Bar>();
. If there's a value you are trying to get it could look like this:
public class Bar: MonoBehaviour {
void Start() {
var foo_script = GetComponent<Foo>();
float some_value = foo_script.some_value;
}
}
If they are on different gameObjects. Do the samething but from accessing that gameObject. If you have a tag on the gameObject called SomethingController
you can do the same thing but with a minor adjustment:
public class Bar: MonoBehaviour {
void Start() {
var foo_script = GameObject.FindWithTag("SomethingController").GetComponent<Foo>();
float some_value = foo_script.some_value;
}
}