- Home /
Activating script by distance problem
I was working on activating a script when the player came x close to the object but, i've encountered a slight problem =/ This is the code im using
function Update () {
var other = gameObject.GetComponent("Script1");
if ( Vector3.Distance(target.position, transform.position ) < 25) {
other.enabled = true;
}
if ( Vector3.Distance(target.position, transform.position ) > 25) {
other.enabled = false;
}
}
It does work however the problem is I need to activate multiple scripts rather then one so i try'd do this...
var target : Transform;
function Update () {
var other = gameObject.GetComponent("Script1", "Script2", "Script3");
if ( Vector3.Distance(target.position, transform.position ) < 25) {
other.enabled = true;
}
if ( Vector3.Distance(target.position, transform.position ) > 25) {
other.enabled = false;
}
}
And it didnt work I also try'd to use multiple var other = gameobject... etc and, that also didnt work I'm currently stuck =/ I'm trying to avoid having to make a script per script needing activated ( 3 scripts need to be activated so i would need 3 of these to activate all 3 )
I would appreciate some help recently i've been trying to work out problems without any help but, on this one im stuck =/
Answer by armoredpokey · Mar 11, 2012 at 01:00 PM
I mean, a very simple answer would be
function Update () {
var other1 = gameObject.GetComponent("Script1");
var other2 = gameObject.GetComponent("Script2");
var other3 = gameObject.GetComponent("Script3");
if ( Vector3.Distance(target.position, transform.position) < 25 ) {
other1.enabled = true;
other2.enabled = true;
other3.enabled = true;
} else {
other1.enabled = false;
other2.enabled = false;
other3.enabled = false:
}
}
Not the prettiest solution, but you certainly don't have to make three different scripts to do what you want.
I try'd that before ( and i try'd yours ) and, it doesn't work =/ ( I replaced the Script1 with my scripts )
Answer by TheLedworks · Mar 11, 2012 at 01:00 PM
You need to use multiple variables, just do the same for...
var script1 : Script = gameObject.GetComponent("Script1");
var script2 : Script = gameObject.GetComponent("Script2");
var script3 : Script = gameObject.GetComponent("Script3");
instead of trying to store three references in one var. Ha ha, how does the expression go? don't put all your eggs in 1 basket :D
Answer by Jacek_Wesolowski · Mar 11, 2012 at 12:56 PM
It looks like you need to call GetComponent()
three times, because it's not smart enough to take multiple arguments. It only accepts one at a time.
You need to store results in three separate variables, eg. other1
, other2
, other3
. If you assign the returned value to other
every time, it will only keep the last value you assigned to it, and not all three. On top of that, you can't declare the same variable more than once (that's what the var
keyword does - it declares variables).
Also, in the interest of better performance, you most likely don't need to call GetComponent()
on every Update()
. Instead, turn other1, other2 and other3 into class fields (rather than local variables) and fill them with values in Start()
. The values will be remembered for as long as your object exists, whereas local variables are lost when the program returns from function.
Your answer
Follow this Question
Related Questions
Terrain shadow distance and lighting problem 0 Answers
Getting a custom class to contain a certain script. 1 Answer
Problem with variables 1 Answer
Help whit Animations play? 0 Answers
AI distance from Player 3 Answers