- Home /
Be able to choose which variable I want from another scripts public variable
I have two scripts, one script has three or more GameObject varibales (Lets call them Thing1, Thing2, and Thing3 etc to Thing15 or higher).
My second script will determine what Thing1, Thing2, and Thing3 is, although this script will be small, so instead of creating 15 scripts or making the code 15 times longer. Is there a way to change it so something like the following would be possible.
First Script:
public class Script1 : Monobehaviour {
public static Script1 scr;
public GameObject Thing1;
public GameObject Thing2;
public GameObject Thing3;
}
And Script2
public class Script2 : Monobehaviour {
//This is where I want to be able to pick any variable from the first script
public (add something here) Choose;
//Where "Choose" would be the same as Scr.Thing1, or Scr.Thing2
public GameObject Obj;
void Start() {
Obj = Choose;
}
}
This way, I can choose what GameObject can set which variable through the public Choose varible.
Comment
Answer by EpiFouloux · Jun 14, 2016 at 12:11 PM
public class Script2 : Monobehaviour {
public Script1 Choose;
public GameObject Obj;
void Start() {
Obj = Choose.Thing1;
}
}