-6
void Update () 
{
    if (Input.GetMouseButtonDown ("Fire1")) 
    {

    }
}

How do I spawn my prefab on click?

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
RagnaLugria
  • 121
  • 2
  • 13
  • 1
    Your question is not clear, but prefabs can be spawned like this: `GameObject instObj = Instantiate(prefab,transform.TransformPoint(Vector3.forward), Quaternion.identity);` – SurvivalMachine Jul 18 '16 at 06:29
  • 3
    Possible duplicate of [Add gameobject dynamically to scene in Unity3d](http://stackoverflow.com/questions/15500137/add-gameobject-dynamically-to-scene-in-unity3d) – Max von Hippel Jul 18 '16 at 06:30
  • https://docs.unity3d.com/Manual/InstantiatingPrefabs.html – nyro_0 Jul 18 '16 at 06:34
  • how do i call my prefab? (called dice) and duplicate it by 10 – RagnaLugria Jul 18 '16 at 06:35
  • e.g. use a for-loop. In the link I have posted there is also a section called "Placing a bunch of objects in a specific pattern". Maybe have a look at it ;) – nyro_0 Jul 18 '16 at 06:40
  • well no one answered my question – RagnaLugria Jul 18 '16 at 06:55
  • 3
    Summing up the comments your question has surely been answered. All it takes for you now is to actually do something yourself – nyro_0 Jul 18 '16 at 07:03

1 Answers1

0

To instantiate a prefab you can use Instantiate (as someone told you in a comment) https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

to do that 10 times use a simple for-loop: for(int i=0; i<10; ++i){ //code }

so, putting all togheter the update functions can be:

void Update () 
{
    if (Input.GetMouseButtonDown ("Fire1")) 
    {
        for (int i = 0; i < 10; ++i){
            Instantiate(m_oMyPrefab, m_oMyPosition, m_oMyRotation);
        }
    }
}

Note that m_oMyPrefab must be a GameObject variable with a reference to your prefab (you can do it programmatically or with the inspector editor), m_oMyPosition must be a Vector3 with the desired position and m_oMyRotation must be a Quaternion. Position and rotation are optionals, see the documentation for more details.

Simone Pessotto
  • 1,561
  • 1
  • 15
  • 19