Pragma strict : GetComponent without knowing the name of the component

Hello,

I try to make my game iOS-compatible so I added #pragma strict at the beginning of every script. That was going well, then I got an error I can’t fix.

First, this is working well with pragma strict :

  var pawnToDestroyScript : PawnScript ;
  pawnToDestroyScript = pawnToDestroy.GetComponent("PawnScript");
  pawnToDestroyScript.pawnBaseReset();

Then I try to get quite the same logic with this one, and here comes the problem :

static function NextStage(stageNumber : int) {
    	var stageToGo : String = "Level" + stageNumber;
    	var tempPlateauObj : GameObject = GameObject.Find("Plateau");
        stageToGoScript : stageToGo = tempPlateauObj.GetComponent(stageToGo) as stageToGo;
    	stageToGoScript.InitLevel();
}

The error is “the name stageToGo does not denote a valid type (‘not found’)”

I tried with this form (as seen on other threads) :

var stageToGoScript : stageToGo = tempPlateauObj.GetComponent.<stageToGo>()  ;

→ not working

I tried to add “as stageToGo” at the end as seen on other threads…
→ not working

Then I tried to change the type of stageToGo :

var stageToGo : String 

→ not working (it was, without pragma strict)

or

var stageToGo : MonoBehaviour

→ not working

or

var stageToGo : Component 

→ not working, nor with GameObject, nor with GameObject.Component nor with Component.stageToGo (I know I’m a noob ;))

→ What is the type of my stageToGo variable ?
It seems I cannot write " var stageToGoScript : stageToGo " since “stageToGo” is not the actual name of the component. The actual name of this component is formed of a string (“Level”) and an integer (stageNumber). (Then I have several script attached to “Plateau”, called “Level1”, another one called “Level2”, etc.)

So what can I do to respect pragma strict with this one ?

I’m new to programming, and I’m sure there are several ways to solve my probleme: I’m eager to learn, so any tip will be welcome :slight_smile:

I played with this some last year, I think it was related to this: Type.GetType Method (System) | Microsoft Learn

So maybe try something like

tempPlateauObj.GetComponent(Type.GetType(“Level3”))

But you may want to consider making all these ‘level’ types subclasses of, say, ‘level’, and then you can use vars of type ‘level’. Although syclamoth’s suggestion is probably less trouble.

Then again, if the intent is to literally change scenes (levels?) they you could use Application.LoadLevel(“Level1”) where Level1 would contain an object with a script with a Startup that initializes that level (and so on for other levels)

I would bypass the issue and use SendMessage; it’s for situations like this.

GameObject.Find("Plateau").SendMessage("InitLevel");

Just as a side note, you generally very rarely would want to use strings with GetComponent, and you don’t need to cast or use extra variables or anything. e.g., instead of

var pawnToDestroyScript : PawnScript ;
pawnToDestroyScript = pawnToDestroy.GetComponent("PawnScript");
pawnToDestroyScript.pawnBaseReset();

Just do

pawnToDestroy.GetComponent(PawnScript).pawnBaseReset();

That’s all you need, although you should follow the convention of using lowercase for variable names and uppercase for class and method names. (Should be “PawnBaseReset”.)

Try this:

static function NextStage(stageNumber : int) {
        var stageToGo : String = "Level" + stageNumber;
        var tempPlateauObj : GameObject = GameObject.Find("Plateau");
        stageToGoScript : MonoBehaviour = tempPlateauObj.GetComponent(stageToGo) as MonoBehaviour;
        stageToGoScript.SendMessage("InitLevel");
}

If you don’t like the SendMessage call, you could make an interface for your level scripts.

interface ILevel {
    function InitLevel();
}

class Level1 implements ILevel {
    function InitLevel() {
        Debug.Log("Level 1 initializing");
    }
}

And then do:

static function NextStage(stageNumber : int) {
        var stageToGo : String = "Level" + stageNumber;
        var tempPlateauObj : GameObject = GameObject.Find("Plateau");
        stageToGoScript : ILevel = tempPlateauObj.GetComponent(stageToGo) as ILevel;
        stageToGoScript.InitLevel();
}