- Home /
Duplicate Question
Changing The Value Of A Variable From Another Script (JavaScript)
I have my script called PlayerAnimations, and a script called ColliderTrigger1. Player animations is like this (javascript):
var parkour1 = false;
function Update ()
{
if (parkour1 == true)
{
animation.Play("parkour1")
}
}
and I want the script ColliderTrigger1 to do something like this:
//Get the parkour1 variable from script PlayerAnimations
function Update ()
{
if (Input.GetKey(KeyCode.G))
{
//parkour1 = true;
}
}
How would i do this. I want to access the variable from the script, then change it to true if player hits 'G'. I just don't understand how to get the variable from the script, then how to change it to true.
Assu$$anonymous$$g your scripts are on separate, unrelated gameobjects, you can use a public & static variable to access it from another script without a direct reference (variable, getcomponent, etc) to that script instance. You can use it just like this (C#):
//In PlayerAnimations
public static bool parkour1 = false;
//In 2nd script
PlayerAnimations.parkour1 = some_value;
$$anonymous$$eep in $$anonymous$$d with a static variable the value belongs to the type itself, not an instance, so if you try to reuse this script on 2+ players at the same time, their static variables will share the same value, and will both be changed when you set it.
The question specified JS, not C#. Also you would not use static variables just to get variables on another script; please don't recommend that since it leads to complications if the person using it doesn't understand what static means.
@infamouslyuseless: this is a massively duplicated question. Every possible variation of this question has been asked and answered many times, and seriously does not need to be asked again since the answer is very easily found with a search. Thanks.
I'm not sure how to properly declare variables in JS but I'm sure he can figure it out - something like this:
public static var parkour1 : bool = false;
As for the approach, I do agree, normally I wouldn't make a variable static just to get to it. I figured it's a pretty simple way to access it assu$$anonymous$$g he only has one instance of his player, but yeah a quick search on the forums will yield a bunch of proper answers for this.
Answer by alexanderflink · Dec 22, 2013 at 04:27 PM
Hi! I am quite new to Unity myself, but I believe this is how you would achieve this. First, you must create a reference to the PlayerAnimations script from the ColliderTrigger1 script, like so:
var playerAnimationsScript: PlayerAnimations;
function Start () {
playerAnimationsScript = GetComponent(PlayerAnimations);
}
Now, you can manipulate the parkour1 variable like so:
function Update () {
if (playerAnimationsScript.parkour1 == "some value") {
playerAnimationsScript.parkour1 = "some other value";
}
}
I get this error : Object reference not set to an instance of an object
Are the two scripts in two different Game objects?
Follow this Question
Related Questions
Scripting error! 1 Answer
Javascript not being updated, variables being overridden, but C# is fine 1 Answer
Assign a script to a variable? 1 Answer
Accessing Script From Other Script Causes Lag? 1 Answer
Prefabs and variables 1 Answer