instantiate 1 only each time not clone all of whats on screen continuoslly

this script does instantiate but when it gets to lets say 8 clones it will then instantiate the exact amount so this is way to many

for example at start it instantiates 1 copy since theres only 1 there but then after it will continue to clone with whatever is there so for example when its has 10 clones it will next start cloning 10 then 20 then 40 then 80 etc I only want it to start at 1 then 1 more then 1 more then 1 more etc

this is to much I ONLY WANT 1 EACH INSTANTIATION please help ive spent days trying to search and figure this out

I do have a few spawn points

2d game

#pragma strict
   
     public var spawnFrequency: float  = 3f; // 3 seconds
 
5.   public var prefab: GameObject;
   var Spawns : Transform[];
 
   function Start () {
 
10.                if (prefab != null)
       {
           InvokeRepeating("SpawnItem", 3.0f, 6.0f );
       }
   }
15.   function SpawnItem () {
       if (prefab != null)
       {
           if (Spawns.Length >= 2)
           {
20.               Instantiate( prefab, Spawns[Random.Range( 2,  Spawns.Length)].position, Spawns[0].rotation);
           }
           else
           {
               Instantiate( prefab );
25.           }
       }
   }

So is this script attached to the gameObject that you instansiate?

1 Answer

1

Well Im not a especially good unity Dev but I would have done something like this:

// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SpawnPrefabController.cs" company="GoDialog">
//  Copyright (C) 2016 Jonas Fjeld
//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using UnityEngine;
using System.Collections.Generic;
using Random = UnityEngine.Random;

public class SpawnPrefabController : MonoBehaviour
{

    /// <summary>
    /// GameObject to Spawn
    /// </summary>
    public GameObject SpawnItem;

    /// <summary>
    /// Is the SpawnController active?
    /// </summary>
    public bool ControllerActive = false;

    /// <summary>
    /// List of the spawned GameObjects
    /// </summary>
    public List<GameObject> SpawnedObjects;

    /// <summary>
    /// How many objects to spawn at one time?
    /// </summary>
    public int SpawnAtOnce = 3;

    /// <summary>
    /// How many objects should be spawned
    /// </summary>
    public int MaxSpawnedObjects = 10;


    /// <summary>
    /// Whats the delay between each spawn group?
    /// </summary>
    public float SpawnEatch = 2;

    /// <summary>
    /// Spawn the full SpawnAtOnce group or just to the spawn cap?
    /// </summary>
    public bool OverSpawn = false;

    /// <summary>
    /// The time left to next spawn group
    /// </summary>
    private float spawnTimeLeft;


    /// <summary>
    ///  Gets a random Possition to spawn the object
    /// </summary>
    private Vector3 spawnPosoition
    {
        get
        {
            return new Vector3(
                Random.Range(0f, 10f),
                Random.Range(0f, 10f),
                Random.Range(0f, 10f));
        }

    }




    /// <summary>
    /// Script initialization
    /// </summary>
	void Start ()
	{
	    // initialize List
	    SpawnedObjects = new List<GameObject>(MaxSpawnedObjects);

	    // Set the spawnTime left so we have a plase to start counting down from
	    // This could be 0f to start spawning right away.
	    spawnTimeLeft = SpawnEatch;
	}
	
	/// <summary>
	/// Update is called once per frame
	/// </summary>
    void FixedUpdate()
    {

         // Check if Objects has been deleted from array
        for (var i = 0; i < this.SpawnedObjects.Count; i++)
        {
            if (SpawnedObjects *== null)*

{
this.SpawnedObjects.RemoveAt(i);
}
}
// Is the controller active?
if (ControllerActive)
{
// Is it time to do a new batch of spawning
if (spawnTimeLeft <= 0)
{
// Is there room for more objects?
if (SpawnedObjects.Count < MaxSpawnedObjects)
{
// Should we Over spawn
if (OverSpawn)
{

// Debug.Log(“Over Spawning " + SpawnAtOnce + " Objects”);
for (int i = 0; i < SpawnAtOnce; i++)
{
// Instantiate Object
var o = (GameObject)Instantiate(SpawnItem, spawnPosoition, Quaternion.identity);

// Set its parrent to this controller
o.transform.parent = this.transform;

// Add Spawned Objects to List
SpawnedObjects.Add(o);
}

}
else
{
var itemsAllowedToSpawn = MaxSpawnedObjects - SpawnedObjects.Count ;
var itemsToSpawn = Mathf.Clamp(itemsAllowedToSpawn,0,SpawnAtOnce);

//Debug.Log(“Spawning " + itemsToSpawn + " Objects”);

for (int i = 0; i < itemsToSpawn; i++)
{
// Instantiate Object
var o = (GameObject)Instantiate(SpawnItem, spawnPosoition, Quaternion.identity);

// Set its parrent to this controller
o.transform.parent = this.transform;

// Add Spawned Objects to List
SpawnedObjects.Add(o);
}

}
}

// We are done for this time so we reset the spawn timer
spawnTimeLeft = SpawnEatch;
// Debug.Log(“DoneSpawning”);

}

// We have to tick down the spawn timer
spawnTimeLeft -= Time.deltaTime;
// Debug.Log(“tick”);
}
}
}

EDIT: Wrote it for 3D just of habit, but just omit the Z-axis, or define a constant for it, and it should work.

ok ill try it also do you know what part of this is regarding the time or lifespan of the spawns/instantiations? I want the instantiations to continue thru forever not stop after a few the script I have now stops spawning after 1-2 minutes