Hello, I keep running into a problem that is making my code ugly. My game is structured so that I have a generic “engine” script that runs on all of the games levels, and a series of scripts that have the data specific to that level. Both of these scripts sit on a game object. It’s easy for me to reference variables when i go from the level level script to the engine, but it is very ugly for me to have to go in the other direction. Here is how I must do it.
in the engine.js:
var stageType:String = "D";
function Start(){
if(stageType=="D")
var levelScript_d:stage_d = gameObject.GetComponent("stage_d");
levelScript_d.doSomething();
}else if(stageType=="F"){
var levelScript_f:stage_f = gameObject.GetComponent("stage_f");
levelScript_f.doSomething();
}
}
I can’t use something like:
var levelScript
on the engine and drag the script onto it because you always have to have the script type in order to do this. Is there a simple way to clean this up so I can always just have a reference to script data.
I want it to look like this:
var levelScript = gameObject.GetComponent("stage_d");
levelScript.doSomething();
…which returns ‘doSomething’ is not a member of ‘UnityEngine.Component’.
Also I realize the minor inefficiencies of the scripts above, but this is just to paint a picture of what I’m looking for. I also realize that the answer is “then don’t structure your game like that.”…but this is something that I’m just interested in knowing the answer to at this point. Thank you! This is one of those weird things that I haven’t been able to google.