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 =/

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.

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 :smiley:

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.