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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -