- Home /
Function to assign gameobject components with parameter strings...
Basically, is there a reasonable way to make a function have a string argument used to assign as a name for a new GameObject?
Like public void thing_maker(string gameobject_name, string sprite_name, string rg_body_name)
I can't get my ideas to work. Anyone have any ideas?
Comment
Answer by Positive7 · Sep 07, 2015 at 04:28 PM
Something like this ? :
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
GameObject cube;
GameObject sphere;
public void newObj(GameObject obj, string Gname, string Gtag, System.Type Gcomp){
obj.name = Gname;
obj.tag = Gtag;
obj.AddComponent (Gcomp);
}
void Update(){
if (Input.GetKeyDown (KeyCode.A)) {
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
newObj(cube, "cube01", "Player", typeof(Rigidbody));
}
if (Input.GetKeyDown (KeyCode.D)) {
sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
newObj(sphere, "sphere01", "Player", typeof(Rigidbody));
}
}
}