GameObject instantiate overlap each other.

hello there.

I’m doing instantiate and manage to do it, but having problem when the gameObject instantiate. The gameObject will overlap each other even I put a min and max range for the gameObject.

How do I tell the gameObject not to overlap each other?

Another question is about the gameObject that being instantiate. As for now I have 3 type of gameObject that I want to instantiate, example : Button1, Button3, Button3. How do I instantiate all the 3 gameObject. If I used Random.Range it will pick either 3 of the gameObject.

Here’s what I’ve done so far:

#pragma downcast
#pragma strict
#pragma implicit

public var button : Transform [];
public var rangeY : float;
public var nRangeY : float;
public var rangeX : float;
public var nRangeX : float;
public var i : int;
public var spawnAmount : int;
public var maxAmount : int;
public var minTime : float;
public var maxTime : float;
public var canSpawn : boolean;

function Awake ()
{
	canSpawn = true;

	if (spawnAmount > maxAmount)
	{
		spawnAmount = maxAmount;
	}

	for (i = 0; i < spawnAmount; i++)
	{
		beforeSpawn ();
	}	
}

function beforeSpawn ()
{
	if (canSpawn)
	{
		yield WaitForSeconds (Random.Range (minTime, maxTime));
		var position : Vector2 = Vector2 (Random.Range (nRangeX, rangeX), Random.Range (nRangeY, rangeY));
		Instantiate (button [(Random.Range (0, 2))], position, Quaternion.identity);
		canSpawn = false;
	}
}

Anyone?

I took some liberty with the code with a little cut and paste so does everything you requested except instantiate three gameobjects ( call it practice for me) :slight_smile:

created a prefab with a rigidbody + gravity
created a empty gameobject and attached the script to this.

#pragma downcast
#pragma strict
#pragma implicit

//public var button : Transform [];
//public var canSpawn : boolean;

public var i : int;
public var spawnAmount : int;
public var minTime : float;

public var minrangeY : float;
public var maxRangeY : float;

var projectile : Rigidbody;
var StartPosition : GameObject;

function Start ()
{
    print ("Starting " + Time.time);
 
        // Start function WaitAndPrint as a coroutine. And wait until it is completed.
        // the same as yield WaitAndPrint(2.0);
        //yield StartCoroutine(WaitAndPrint(minTime));

    for (i = 1; i < spawnAmount; i++)
    {
        WaitAndPrint(1.0f);
        yield WaitForSeconds (minTime);
    }    
}

function WaitAndPrint (waitTime : float) {

    var cubicTransform = StartPosition.transform; 

    var xx = cubicTransform.position.x + Random.Range (minrangeY, maxRangeY);
    var yy = cubicTransform.position.y;
    var zz = cubicTransform.position.z;

    var clone : Rigidbody;

    //clone = Instantiate(projectile, cubicTransform.position, transform.rotation);

    clone = Instantiate(projectile, Vector3(xx, yy, zz), Quaternion.identity);
    yield WaitForSeconds (waitTime);
    print ("WaitAndPrint "+ Time.time);
}

@runner.

Thank, will test your code and will update you later.

@runner. Okay i’ve tried your code.

what does it do actually. What is projectile? and what is Start position? why must there be a rigidbody in my button? would that make my button fall.

Does the projecitile project? sorry I’m a bit confuse.

I believe your object indeed needs a rigidbody to make it the script’s projectile. If you dont want it to fall down, disable the gravity option in the inspector

In the startfunction the code will spawn an object as long as the amount of spawned objects is lower than the maximum spawned objects (which is set in the inspector) If it is, it will spawn a clone of the projectile object.

rigidbody prevents objects from going through things

Start Position is an empty GameObject in world space and that the projectile(clone) will instantiated at that position in world space with some randomness.

In the inspector you can can vary how many are spawned and how fast you want them spawned and the randomness spread in world space from the center of Start Position.

Hope you find it of some use but i will leave the rest up to you. such as the 3 button variation

803888–29587–$Spawner.zip (156 KB).

I have provided a Project Sample to help in case its needed

@runner, thanks for the info but unfortunetly thats not what I intended to do.

What Im doing is something like this, see picture below :

When my game start, for first few random spawn all the button will not overlap each other,

but then, after a while my button will overlap each other,

this is my script that I used to call my random spawn :

#pragma downcast
#pragma strict
#pragma implicit

//BUTTON GAMEOBJECT//
public var button : Transform [];
//FLOAT AREA//
public var rangeY : float;
public var inRange : float;
public var rangeX : float;
public var minTime : float;
public var maxTime : float;
//INT AREA//
public var i : int;
public var spawnAmount : int;
public var maxAmount : int;
//BOOL AREA//
static var canSpawn : boolean;

function Awake ()
{
	//THIS WILL TELL YOU IF YOU CAN SPAWN OR NOT//
	canSpawn = true;	
}

function Update ()
{
	if (canSpawn == true)
	{
		//CONSTRAIN YOUR AMOUNT OF SPAWN//
		if (spawnAmount > maxAmount)
		{
			spawnAmount = maxAmount;
		}

		for (i = 0; i < spawnAmount; i++)
		{
			BeforeSpawn ();
		}	
	}

	if (canSpawn == false)
	{
		i = 0;
		gameObject.active = false;
	}
}

function BeforeSpawn ()
{	
	if (canSpawn)
	{
		//DESTROYING TOUCH INSTATIATE//
		var destroyMe : GameObject;

		destroyMe = gameObject.FindWithTag ("destroyMe");
		Destroy (destroyMe);
		//DESTROYING TOUCH INSTATIATE//

		yield WaitForSeconds (Random.Range (minTime, maxTime));

		var position : Vector3 = Vector3 (Random.Range (-rangeX, rangeX) + Random.Range (-inRange, inRange), Random.Range (-rangeY, rangeY) + Random.Range (-inRange, inRange), -1.714075);
		Instantiate (button [0], position, Quaternion.identity);

		var position2 : Vector3 = Vector3 (Random.Range (-rangeX, rangeX) + Random.Range (inRange, -inRange), Random.Range (-rangeY, rangeY) + Random.Range (inRange, -inRange), -0.79795);
		Instantiate (button [1], position2, Quaternion.identity);

		var position3 : Vector3 = Vector3 (Random.Range (-rangeX, rangeX) + Random.Range (inRange, inRange), Random.Range (-rangeY, rangeY) + Random.Range (inRange, inRange), 0);
		Instantiate (button [2], position3, Quaternion.identity);

		canSpawn = false;
	}
}

How do I make my button not overlap each other.

830862--30958--$random1.jpg
830862--30959--$random2.jpg

bump

anyone?