- Home /
Pass value from several objects into one JS
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
Answer by Starwalker · Nov 08, 2013 at 08:26 PM
function Awake()
{
printValue = GameObject.FindGameObjectWithTag("GameController").GetComponent(z_PrintValue);
}
You are referencing the same script in both scripts, make the name as "z_PrintValueLeft" and "z_PrintValueRight" and add this to the same object. At a given time, if there are multiple script clones on the same object, only 1 is returned, your other script is never part of the return hence it wont work.
In Theory: Using exact same variable (field) on a common point of return will need to be seperated by some event which happens sequentially. A variable referenced in an Update function will get updated which ever calls it 1st, and then stay untill another event updates it. Both will never happen simultaneously effecting the same variable. Simultaneous input is possible, but not on the same variable name.
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...
inputValue = inputHorizontal;
, inputValue should be applied to Player then, this makes inputHorizontal
independent and will always have a value applied.