How to instantiate multiple instances of same game object

Hello everyone,

I want to know how to create, instantiate multiple instances of the same game object (or prefab) since its script component c #,
without referencing the same for all.

Basically I would like to generate and duplicate very quickly thanks to the script code number (hundreds) of game object at different positions (with several variations of components) field of the scene from a single 3d model imported from box (one Game object base)
(Using Table list or whatever …)

Thank you and good day.

Cordially.

1 Like

You can use a loop to create multiple instances simultaneously:

public Transform[] yourPrefabVariations;
public int instanceCount = 100;

// You might even want to keep track of each instance:
private List<Transform> _instances = new List<Transform>();

private void CreateInstances() {
    for (int i = 0; i < instanceCount; ++i) {
        // Randomly pick prefab variation:
        var yourPrefab = yourPrefabVariations[ Random.Range(0, yourPrefabVariations.Length) ];

        var instance = Instantiate(yourPrefab) as Transform;
        _instances.Add(instance);

        // Then adjust your position as needed.
        instance.localPosition = .....
    }
}

Without an explanation of your intentions it is very hard to know what to suggest; but hopefully this will help getting you started :slight_smile:

2 Likes

Take a look at http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

The change you would need to make is to keep a reference to the Transform returned and store it in an array or List (or whatever collection object you want)

thank you again.

But I would like to know why the cloned objects appear only during rendering by pressing play .
but not in the scene or in the hierarchy, or in the panel game …
thank you and good afternoon.
see you soon.

please Refer the above image

using UnityEngine;
using System.Collections;

public class cube : MonoBehaviour {

	public GameObject player;
	float speed = 5;


	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
		float movement = Input.GetAxis("Horizontal");

		Instantiate(player,transform.position - new Vector3 ( 3, 0 , 0 ),Quaternion.identity);
		float dirX = movement*speed*Time.deltaTime;
		Vector3 finalTranform = new Vector3(dirX,0,0); 
		transform.Translate(finalTranform);

	}
}

Just create the reference of the object you want to instantiate in inspector using public variable

and refer to Unity - Scripting API: Object.Instantiate

Hello,
Thank you for your help and sorry for my very late reply
(I rarely connects on the forum).
Problem solved.

Hi every one :slight_smile: ,

I would like you to help me on a problem of unity code
I instantiate a wide range of platforms with constant distance therebetween 50.0F,

I would like to make several different animations these platforms (eg for each x a single translation) so on.

The problem:

the plateormes with x = 5, 6, 9 does not lift from Table5 namely, 6.9.

When I add at the end grille1 [a1] rotate (2f, 2f, 2f).;

this problem then dissipates, but leaving room to another:

platforms with y = 10,11,12,13,14,15 not lift from corresponding in principle to Table10, 11 → 15.

According to you, what are the errors in the code.

Thank you in advance for your help.

Good day and see you soon.

Cordially.

here are thev code in second message

You might want to consider placing all those arrays inside a collection!!

For instance:

List<GameObject[]> grilles = new List<GameObject[]>();
for (int i = 0; i < 27; ++i)
    grilles.Add(new GameObject[nombre3];

Hello,
Thank you.
I have now 4 lists all the tables.
But after, I do not know how to use specific translation table for each list
“Tableaux”, because in fact I compare the position of these indices 0 tables.
How in this case?
Thank you again

Hi, I’ve got a problem . I have a gameobject with multiple materials…such as “Element 0, Element 1, Element 2 …etc”. I’ve added an offset changer script to that gameobject but the script is only working with the first material i.e “element 0” . I dont want that material to be worked with that script but another material i.e “element 2”.
On the first Element, the material is signed as “Instanced” …I think If I make the material of “element 2” as Instanced , it will work . but how can i particularly Instance that Material that I’ve choosen among the multiple materials of that Object?
Please , I need a solution for this.