Martin j
Martin j

Reputation: 521

infinitely generating game object in unity

using UnityEngine;
using System.Collections;

public class instantiatescript : MonoBehaviour {

public GameObject house;

// Use this for initialization
void Start () {
     house = GameObject.FindGameObjectWithTag ("house");
    CreatePrefab ();
}

// Update is called once per frame
void Update () {


}


void CreatePrefab()
{



    for (int i = 0; i < 10; i++)
        Instantiate(house, new Vector3(i * 0f, 0f, 2.0f), Quaternion.identity);


}
}

i need to generate the game object house infinitely along the z direction.but now this code actually does nothing ,can any one provide a reference for the infinite generation of object

Upvotes: 2

Views: 306

Answers (1)

Andrea
Andrea

Reputation: 6123

The logic is somewhat right, but:

  • house doesn't need to be initialized with an instantiated GameObject. Just leave the declaration as is and assign a prefab through the Inspector at the variable;
  • That for cycle should consider multiple x values and generate a house at each iteration. But what are you expecting by multipling an arbitrary i by 0? The coordinates will remain always the same. Maybe you want to add?

Upvotes: 2

Related Questions