ok so i have my scene with a castle on either side, and i have an object in the hierarchy called “Swordsman” when i press on a button i have, i want it to spawn/create that Swordsman object at a x,y position.
if (GUI.Button (Rect (Screen.width/2-50, Screen.height/2-60, 100, 20), "Swordsman") {
}
Whenever I want to instantiate an object, I create a GameObject property on the script class so i can drag it over (though you can also grab it via code as well). Creating/positioning it is pretty simple:
// Create a new instance of an object
GameObject obj = (GameObject)GameObject.Instantiate(Swordsman);
// Position it where we please
obj.transform.position = new Vector3((float)x, -(float)y, 0.0f);
// Put the object in this object (the object this script is attached to)'s child hierarchy
tile.transform.parent = transform;
So what I then do is I have a public GameObject myType; definition for the class. I can then drag a prefab in unity to that property in the inspector.