I want to spawn 4 game objects in my chosen positions when a Button is clicked.Unity 2D C#
Asked
Active
Viewed 54 times
-3
-
1What point are you at currently? Do you have a button which calls a function yet? If so, then get references to the prefabs you want to spawn in, call [Instantiate](https://docs.unity3d.com/ScriptReference/Object.Instantiate.html) on each of them and spawn them in at specified [Vector2](https://docs.unity3d.com/ScriptReference/Vector2.html) locations – AidanH Jul 30 '18 at 09:37
-
2Can you add some code and show us what you've tried so far? – Shogunivar Jul 30 '18 at 09:44
-
1Instantiate(yourPrefabObject, yourTranformPossition); – Ivan Kaloyanov Jul 30 '18 at 13:28
1 Answers
1
Edit: Apparently I didn't understand your question right. if your button object uses a collider. you can detect mouse events on that object. assign this script to your object that has collider.
public Transform prefabToCreate;
public Vector2 posToCreate = Vector2.zero;
void OnMouseDown() {
Instantiate(prefabToCreate, posToCreate, Quaternion.identity);
}
It's easy to figure out how to make the object 4 times.
p.s: you should explain your question in more detail.

Amin
- 543
- 7
- 12
-
-
1OP said 'when a button is clicked', which is not the same as Input.GetMouseButtonDown – AidanH Jul 31 '18 at 07:37
-