Basically is there an actually time saving way to creat a function that lets you create a GameObject specified by the function's parameter(s)?
like:
public void thing_maker(string gameobject_name, string sprite_name, string rg_body_name)
Basically is there an actually time saving way to creat a function that lets you create a GameObject specified by the function's parameter(s)?
like:
public void thing_maker(string gameobject_name, string sprite_name, string rg_body_name)
this example should do what you need. it requires that you have a folder named Resources and that the sprite you want to load will be inside that folder.
another option is doing GameObject go = Instantite(SomePrefabName) as GameObject
insted of new GameObject()
, in case you have a prefab which is ready and you only want to maybe change some of its components' values.
Good luck.
public GameObject thing_maker(string gameobject_name, string sprite_name, string rg_body_name)
{
GameObject go = new GameObject(gameobject_name);
SpriteRenderer sr = go.AddComponent<SpriteRenderer>();
sr.sprite = Resources.Load<Sprite>(sprite_name);
Rigidbody rb = go.AddComponent<Rigidbody>();
/* now if you want to change values of the components you can just do it
by accesing them directly. for instance: rb.isKinematic = true; will
change the isKinematic value of this rigidbody to true. */
return go;
}
It seems to me that you could try something like this:
public void ThingMaker(string gameobject_name, Sprite spriteToDraw) {
GameObject newObj;
newObj= new GameObject(gameobject_name);
newObj.AddComponent<Rigidbody2D>();
newObj.AddComponent<SpriteRenderer>();
newObj.GetComponent<SpriteRenderer>().sprite = spriteToDraw;
}