Hi. Hello. How do I change a variable of a script from another script? The variable I have to change, however, must not be static.
If the objects the scripts are on exist at editor time, you can link them together in the Inspector
ScriptA.jsvar myVariable : int;
ScriptB.js
var otherScript : ScriptA;
function Start()
{
otherScript.myVariable = 10;
}
Put ScriptA on one object, ScriptB on another, then drag ScriptA’s object onto ScriptB’s “otherScript” variable in the inspector.
Otherwise, you’ll probably need to use GetComponent. The trick to remember is every script you write is a component. (check out the examples in that link!)
I Get NullReferenceExeption
the script
[/CODE]var obj1 : Transform; //the gameobject (Clip)
var obj2 : Transform; //my Player
var Bar : float;
var Amount = 60;
var script : gunscript;
function Start ()
{
var Bar = (Screen.width / 1);
other = obj1;
}
function Update()
{
}
function OnTriggerStay(other : Collider)
{
if(other.CompareTag(“Player”))
TakeClip();
}
function TakeClip()
{
if(Input.GetKeyDown (“f”))
{
AddClip(Amount);
}
}
function AddClip(Amount)
{
script.clips += Amount;
DestroyClip();
}
function DestroyClip()
{
Destroy(gameObject);
}
function OnGUI() //function for Gui
{
var dist = Vector3.Distance(obj1.position,obj2.position);
if(dist < 2)
{
GUI.Box (new Rect(10, 740, Bar, 40), “Ammo for You
, F to pickup!”);
}
}[/CODE]
and the variable is “clips”.
I can’t put in the variable script the variable gunscript.
Is your gun script called gunscript.js ?
Yes
It work with this gunscript.AddedClips += Amount; and clips += AddedClips; .I want it when It add the shots then stops add but that continues to add to each frame.
I want it finished to add when the variable becomes false isTaking so the script gunscript … if (isTaking == true) {clips + = AddedClips}.
P.S. Initially the variable AddedClips is 0, and I would like the variable AddedClips back to 0 so when I get more ammo it reaches 60.
“var Bar” in Start doesn’t set your global Bar variable - it creates a new one only in scope of the Start method. Lose the var in front of it. Also in Start - what is other?