- Home /
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
Answer by shadowriffe · May 19, 2012 at 01:34 AM
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.
That is a good idea, but the initial mission script variables are all booleans. I'm trying to get the npcs to check for a specific boolean in that script, so that if it is false, they say or do specific things until that boolean is true. this method would allow me to use one universal script for npcs because I would be able to easily name which variable to check for in the inspector
Answer by AchillesWF · May 19, 2012 at 01:33 AM
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.
Could you give an example please? I looked through here, but i'm confused as to do 'GetComponent.(a String Name)'
You have to use something like: GameObject.Find("ObjectName").GetComponent(ComponentType)
Another option would be to add a $$anonymous$$ission tag to your mission Object. You will need to do some kind of Find before you can do a GetComponent.
Answer by save · May 19, 2012 at 01:42 AM
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[];
Your answer
Follow this Question
Related Questions
Can I use strings with static variables? 2 Answers
Inputfield set as Variable 1 Answer
How to get variable from another unknown script ? 1 Answer
GetComponent with variable script possible? 1 Answer
Passing a Script Name to a Function 2 Answers