Hello.
I am struggling with something here and i got little bit confused.
I want to pass (continously) a value from several different objects into one. Let’s say, that i’m making side scrolling game and i have 2 buttons for running left and right. My player have a variable “inputHorizontal”, which depending of it’s value (1 or -1) will make the player run left / right.
Now i have to pass 1 from one of the buttons and -1 from the other into that same variable “inputHorizontal” and in the same time if i press one button and release it, inputHorizontal, shoud go back to 0.
For some reason only one of the buttons work and pass value to the variable and only if i deactivate it, the other one starts working.
For the purpose i’ve made simple scripts, so i don’t need to copy tons of code here.
Here’s the 2 buttons scripts, which are identical except the value passes 1 or -1.
Script name : MoveButtonLeft_JS
#pragma strict
// Public Variables
public var inputHorizontal : float;
// Private Variables
private var printValue : z_PrintValue;
private var onPress : boolean;
function Awake()
{
printValue = GameObject.FindGameObjectWithTag("GameController").GetComponent(z_PrintValue);
}
function Update()
{
printValue.inputHorizontal = inputHorizontal;
if (onPress)
inputHorizontal = 1;
else
inputHorizontal = 0;
}
function OnMouseDown()
{
onPress = true;
}
function OnMouseUp()
{
onPress = false;
}
Here’s the second button script
Script Name: MoveButtonRight_JS
#pragma strict
// Public Variables
public var inputHorizontal : float;
// Private Variables
private var printValue : z_PrintValue;
private var onPress : boolean;
function Awake()
{
printValue = GameObject.FindGameObjectWithTag("GameController").GetComponent(z_PrintValue);
}
function Update()
{
printValue.inputHorizontal = inputHorizontal;
if (onPress)
inputHorizontal = -1;
else
inputHorizontal = 0;
}
function OnMouseDown()
{
onPress = true;
}
function OnMouseUp()
{
onPress = false;
}
and this is the script, that, the above 2 buttons shoud feed the value to. This script is attached to game object, that is tagget as “GameController”:
Script Name: z_PrintValue
public var inputHorizontal : float;
function Update()
{
print (inputHorizontal);
}
Why only one o of the scripts is passing information and the other one doesn’t :\
thanks ![]()
inputHorizontal
thanks, but i have to (somehow) pass from the 2 buttons, into that variable. The thing is that my Player have a variable "inputHorizontal". If that variable is 1 the player runs right, if it's -1, the player run's left. These 2 buttons will make him run left or right. I can't add 2 times the script of for the Player Controller...
– instruct9r
– StarwalkerinputValue = inputHorizontal;, inputValue should be applied to Player then, this makesinputHorizontalindependent and will always have a value applied.