- Home /
The question is answered, right answer was accepted
Instantiate sometimes creates more then 1 objects
I am trying to create 1 point at a time every few seconds on a random basis of 1-5 seconds and the problem im curently facing is that i am sometimes creating 2 objects at the same time even after i added a bool to make sure the instantiate code is only ran once everytime. Here is my code:
if(only1 == true) { GameObject Point1 = Instantiate(PointArray[(Random.Range(0, PointArray.Length))], PointSpawner1.position, Quaternion.identity) as GameObject; Point1.rigidbody2D.velocity = transform.TransformDirection(new Vector2(-1, 0) * 5); only1 = false; } yield return new WaitForSeconds(Random.Range (1,5)); if(only2 == true) { GameObject Point2 = Instantiate(PointArray[(Random.Range(0, PointArray.Length))], PointSpawner2.position, Quaternion.identity) as GameObject; Point2.rigidbody2D.velocity = transform.TransformDirection(new Vector2(-1, 0) * 5); only2 = false; } yield return new WaitForSeconds(Random.Range(1,5)); only1 = true; only2 = true;
This code is ran in a loop forever.Please help me.Thank you
Are you sure this script is only attached to one object?
that only1/2
stuff is unnecessary. the code won't create more than one at a time and will wait for between 1 and 5 seconds... i created a test script as proof. as mentioned, maybe you have it attached to more than one object.
using UnityEngine;
using System.Collections;
public class SingleInst : $$anonymous$$onoBehaviour
{
public GameObject gameObj1, gameObj2;
private Vector3 pos1 = new Vector3(0,0,0);
private Vector3 pos2 = new Vector3(1,0,0);
public void Awake()
{
StartCoroutine(GenerateObjects());
}
private IEnumerator GenerateObjects()
{
while (true)
{
GenerateObj(gameObj1, pos1);
yield return new WaitForSeconds(Random.Range(1, 5));
GenerateObj(gameObj2, pos2);
yield return new WaitForSeconds(Random.Range(1, 5));
}
}
private void GenerateObj(GameObject obj, Vector3 pos)
{
var point = Instantiate(obj, pos, Quaternion.identity) as GameObject;
if (point != null)
{
point.rigidbody2D.velocity = transform.TransformDirection(new Vector2(-1, 0) * 5);
}
Debug.Log(Time.time);
}
}
i checked twice and the script is on only one object the main camera. the script works good at first but after a while it generates like 2 objects on top of each other
i'm not understanding what you expect this to do versus what it is doing. the code performs as written. maybe you can post some screenshot of what you see (showing the incorrect spawning). my tests with the info you gave all do what they're supposed to.
can you print all of the debug log for each spawn... this modified code will do it, along with showing gizmos on the spawn points (they're fairly close to each other)
using UnityEngine;
using System.Collections;
public class SingleInst : $$anonymous$$onoBehaviour
{
private readonly Vector3 _pos1 = new Vector3(8.5f, 3.7f, -4.0f);
private readonly Vector3 _pos2 = new Vector3(8.5f, 3.0f, -4.0f);
public GameObject NormalPoint, HighPoint, $$anonymous$$egaPoint;
private GameObject[] _pointArray;
public void Awake()
{
_pointArray = new [] {
NormalPoint, NormalPoint, NormalPoint, NormalPoint, NormalPoint,
HighPoint, HighPoint, HighPoint, HighPoint,
$$anonymous$$egaPoint
};
StartCoroutine(GenerateObjects());
}
private IEnumerator GenerateObjects()
{
while (true)
{
GenerateObj(_pos1);
yield return new WaitForSeconds(Random.Range(1, 5));
GenerateObj(_pos2);
yield return new WaitForSeconds(Random.Range(1, 5));
}
}
private void GenerateObj(Vector3 spawnPos)
{
var point = Instantiate(_pointArray[(Random.Range(0, _pointArray.Length))], spawnPos, Quaternion.identity) as GameObject;
if (point != null)
{
point.rigidbody2D.velocity = transform.TransformDirection(new Vector2(-1, 0) * 5);
Debug.Log(Time.time + " at " + point.transform.position);
}
else
{
Debug.Log("Instantiated failed!");
}
}
public void OnDrawGizmos()
{
AddDebugGizmoSphere(_pos1, 0.3f, Color.red);
AddDebugGizmoSphere(_pos2, 0.3f, Color.green);
}
private void AddDebugGizmoSphere(Vector3 pos, float radius, Color color)
{
Gizmos.color = color;
Gizmos.DrawSphere(pos, radius);
}
}
yes.... and i wasted so much time with this thank you :)
Answer by drudiverse · Jul 21, 2014 at 11:06 AM
You PointSpawner1 and 2 positions are not Random.
They are obviously drawing from the same source of positions and sometimes have the same.
what are pointspawnder1 and 2 code?
i have 2 transforms set point spawner 1 is on a high position and point spawner 2 is on a low position ...but it seems like most are spawning on a top position even tho the code says you spawn from first wait and spawn from second