- Home /
Calling Other Variables from Other Scripts.
So, as the title says, I have a slight dilemma regarding calling variables from other scripts. So, I have one script, where you can switch objects, and when one object is selected, then, it should call on another script, a move script, to change the speed. So, here are my scripts:
var Obj : Transform;
function Start () { // Select the first weapon SelectWeapon(0); }
function Update () { // Did the user press fire? if (Input.GetButton ("Fire2")){ print("YO!"); }
if (Input.GetKeyDown("1")) {
SelectWeapon(0);
}
else if (Input.GetKeyDown("2")) {
SelectWeapon(1);
//this is where I would call the var to change the runspeed
}
else if (Input.GetKeyDown("3")) {
SelectWeapon(2);
}
}
function SelectWeapon (index : int) { for (var i=0;i
pragma strict
var walkSpeed: float = 7; // regular speed var crchSpeed: float = 3; // crouching speed var runSpeed: float = 20; // run speed
private var chMotor: CharacterMotor;
private var tr: Transform;
private var dist: float; // distance to ground
function Start(){
chMotor = GetComponent(CharacterMotor);
tr = transform;
var ch:CharacterController = GetComponent(CharacterController);
dist = ch.height/2; // calculate distance to ground
}
function Update(){
var vScale = 1.0;
var speed = walkSpeed;
if (chMotor.grounded && Input.GetKey("left shift") || Input.GetKey("right shift")){
speed = runSpeed;
}
if (Input.GetKey("c")){ // press C to crouch
vScale = 0.5;
speed = crchSpeed; // slow down when crouching
}
chMotor.movement.maxForwardSpeed = speed; // set max speed
var ultScale = tr.localScale.y; // crouch/stand up smoothly
tr.localScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5*Time.deltaTime);
tr.position.y += dist * (tr.localScale.y-ultScale); // fix vertical position
}
Any help would be greatly appreciated. Thanks in advance!
Answer by ransomink · Jan 15, 2015 at 01:54 AM
First off, no one can help if you can't properly tell us the names of your scripts. Adequate info is necessary. Secondly, @ThomasMarsh gave you the correct answer for accessing a variable from another script.
Inside of the script called "CharWeapon" (the actual script you are calling from)
"The other script which I call from is called something like charweapon or something like that."
create a private variable with the same name as the script you want to access, but in lowercase.
private var move : Move; // Reference to the Move script
Then setup the instance to the script in the Awake () function.
function Awake ()
{
move = GetComponent ( Move ); // Set a reference to the Move script
}
In your code you have...
move = GetComponent ( Player );
... which is incorrect because "Player" is the gameobject, not the script you're trying to access. There are also other factors at play: the "CharWeapon" and "Move" script must be attached to the "Player" gameobject, and "runSpeed" has to be a public variable in order to access it.
You do not have to cast a type to "runSpeed", you already - should have anyway - done so inside the "Move" script. Also, using 50.0f is C# syntax; with UnityScript it is not needed. The code should be as follows...
move.runSpeed = 50.0;
After reading the rest of my comments, you should see that I updated. But it still doesn't work.
Post both of your scripts, so we can see all lines. I tested this and it works 100%. Hopefully we'll see where the issue lies...
Answer by ThomasMarsh · Jan 14, 2015 at 04:39 AM
The syntax I personally use when accessing other scripts is this:
private var enemySight: EnemySight;// EnemySight is name of the script
function Awake(){
enemySight = GetComponent(EnemySight);// A reference to get script from object it is in
}
And then to access a public variable from that script, you would use enemySight.playerInSight where playerInSight is a variable in the EnemySight script. It must be public to access it. I hope this is of use to you.
So, here's what I changed it to (it doesn't work-no errors, but nothing really changes...note that I changed the float from 20 to 50...):
var obj : Transform;
private var move : $$anonymous$$ove;
function Start () { // Select the first weapon SelectWeapon(0); }
function Awake() {
move = GetComponent(Player); }
function Update () { // Did the user press fire? if (Input.GetButton ("Fire2")){ print("YO!"); }
if (Input.Get$$anonymous$$eyDown("1")) {
SelectWeapon(0);
}
else if (Input.Get$$anonymous$$eyDown("2")) {
SelectWeapon(1);
move.runSpeed : float = 50.0f;
}
else if (Input.Get$$anonymous$$eyDown("3")) {
SelectWeapon(2);
}
}
function SelectWeapon (index : int) { for (var i=0;i
In the player script, try changing the run speed variable to public.
Also, is player the name of the script? Whart are the names of the two different scripts?
No, player is the name of the gameobject that the script is in. The actual script is called "$$anonymous$$ove". That's the one I want to call. The other script which I call from is called something like charweapon or something like that. Do you know if you could apply the answer as an example in my script?
Answer by ramp · Jan 14, 2015 at 05:01 AM
Hello,
Accessing the variable from one to another script,then first you find the name of GameObject in Hierarchy and then after that script name (i.e GetComponent),just like this.
chMotor = GameObject.Find("").GetComponent("CharacterMotor");
may be help.
Thanks
Your answer
