I am writing a script, currently it is getting a little big in my opinion (around 500 lines of code)
It was hard to make, so I won’t post it here. My question is… I am trying to use GetComponent to get a variable from within another script. The problem is, I want to be able to change which script to get it from using the script as a variable… Example
`var script : ???;
gameObject.GetComponent(script).variable
if(2 == 2){
script = otherscript
}`
See my dilemma? I don’t know what type to use for my variable. I thought MonoBehavior, but it throws tons of errors! It would say “variable is not a member of UnityEngine.Component”
So can someone help me out, please?
Does the script variable containing the script have to be “global”? Or could you do something like this?
function yourFunction (whichScriptToUse : int) : float {
var theScript;
switch(whichScriptToUse) {
case 0:
theScript = gameObject.GetComponent(script);
break;
case 1:
theScript = gameObject.GetComponent(otherscript);
break;
}
return theScript.variable;
}
function Start () {
// using script2s variable
var variableFromScript : float;
variableFromScript = yourFunction(0);
Debug.Log("Variable in Script1: " + variableFromScript);
variableFromScript = yourFunction(1);
Debug.Log("Variable in Script2: " + variableFromScript);
}
I suggest you create an abstract class inherithing from MonoBehaviour class and two (or more) classes inheriting from it. In the script in your question, deal with the abstract class and implement the particular behaviour inside the children class. If it’s not clear, tell me and I will add an example.
I had #Pragma Strict on. Without it, everything works fine. I feel like an idiot now.
THANKS EVERYONE FOR ALL OF YOUR HELP!!!