GetComponent from string name?

is it possible to make a GetComponent command read a string variable for another script?

ie

`var CurMission: String;

function Start(){

if(player.GetComponent(MissionScript)…CurMission… == true){

//do something

}`

This is going on a more detailed script I’ve made for npcs.
I am going to have multiple missions with many different variable names- I would like to be able to input those specific variable names through the inspector, if possible.

any help is appreciated, thanks

I don’t think you can overload GetComponent with a string, so having it fill that in at runtime would not work.

Here is a snippet of code that might help depending on what you need to do,which I can’t really understand, and I know it’s not super efficient. It uses a string to search the heirarchy of a tranform to look for a transform with a specific name.

static function FindTransform(parent : Transform, name : String) : Transform{
if (parent.name == name) return parent;
var transforms = parent.GetComponentsInChildren(Transform);
for (var t : Transform in transforms)
{
if (t.name == name) return t;
}
return null;
}

So I could see you have a GameObject in a character called Mission_FetchQuest2 and your character could have a list of strings that denote active quests, you could look for “Mission_FetchQuest2” in the quest-giving NPC, and it would return the corresponding transform/object.

Sure you can. See this:

http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

Just don’t pile up the functions too heavily in a single expression without checking the return value to make sure it isn’t null, or else prepare to do a lot of work tracking down null references! Once you have an object, you can get at any of its components, but make sure you understand casting properly (if you are going to use C#) so that you can take return values and get them to be objects that have the variables you are looking to access.

I’m not sure I understand the string solution, is this to compare what variables are named? It’s a good question but I don’t see a direct solution programmatically for that. An enumeration might be of delicate use instead!

//Player
enum MISSIONTYPE {
    Driving,
    Hanggliding,
    DrinkBeer,
    Cooking
}

var mission : MISSIONTYPE = MISSIONTYPE.Driving;
static var playerMission : MISSIONTYPE;

function Start () {
    switch (mission) {
        case MISSIONTYPE.Driving: /*Set vars for driving*/ break;
        case MISSIONTYPE.Hanggliding: /*Set vars for hanggliding*/ break;
        default: MISSIONTYPE.DrinkBeer: break;
    }
    playerMission = mission;
}

//Get the enum
function WhatMissionIsThis () {
    var mission : MISSIONTYPE = PlayerScript.playerMission;
}

You could also make a class to better organize values between missions

class MissionClass {
    var name : String;
    var type : MISSIONTYPE;
    //and so on
}
var missions : MissionClass[];