c# - Using GetComponent to get a script using a string -
using following code able script "smg" , apply weaponobject:
weaponobject = gameobject.findgameobjectwithtag(smg).getcomponent<smgscript>();
is possible action following , if so, how?
string variable = "smg"; weaponobject = gameobject.findgameobjectwithtag(variable).getcomponent<variable>();
i want have number of scripts can apply weaponobject dependant on variable.
since see weapon has different name script need 2 variables.
string variable = "smg"; string scriptname = "smgscript"; gameobject.findgameobjectwithtag(variable).getcomponent(scriptname);
this not efficient.
solution
what want have parent class , weapons inherit it.
public interface weapon { }
then create weapons example:
public class m4 : monobehaviour, weapon { } public class mp5: monobehaviour, weapon { }
when ever want grab script component can use this:
string tag = "weapon"; weapon w = gameobject.findgameobjectwithtag(tag).getcomponent(typeof(weapon)) weapon;
Comments
Post a Comment