Script Spawning Double?
I am trying to work on a script to spawn a gameobject at a random position between a range of positions but not too close to each other. It is spawning, just not correctly. It is spawning 2 sets of 12 random positions instead of just 1 set. Each set is following the correct range rules, however when the 2 sets are put together, they don't follow the range rules and are overlapping. I am unsure where I went wrong with this script and would love to get some help with it.
Here it is: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SpawnTest : MonoBehaviour
{
public GameObject prefab;
public int wayPoints = 12;
public float minDist = 1f;
public List<Vector3> waypointPositions = new List<Vector3>();
void Start ()
{
FillList();
}
void FillList ()
{
for (int i = 0; i < wayPoints; i++)
{
while (waypointPositions.Count < wayPoints)
{
Vector3 randomPos = new Vector3(Random.Range(-2f, 2f), Random.Range(.5f, 4f), 0f);
bool isClose = false;
foreach (Vector3 wp in waypointPositions)
{
if (Vector3.Distance(wp, randomPos) <= minDist)
{
//Debug.Log("dist: " + Vector3.Distance(wp, randomPos));
isClose = true;
}
}
if (!isClose)
{
waypointPositions.Add(randomPos);
Instantiate(prefab, randomPos, prefab.transform.rotation);
break;
}
}
}
}
}
When putting wayPoints = 6, I get 18 actual wayPoints. When putting wayPoints to 12, I get 24 waypoints. When putting wayPoints to 24, I get 12 wayPoints.
Are those numbers predictable? You can test that by rerunning several times.
Could it be something that happens inside the prefab you are instantiating? You can test that by trying this script with a different prefab.
It's hard to understand your intent. It was hard for me, at least. It might be that there's something you think you are doing that you are not really doing or something you are doing that you think you aren't really doing. Try refactoring this method into a series of private method calls with intention-revealing names. Try refactoring the private methods the same way, if it helps add clarity.
The prefab I am using is a basic cylinder, no extras added. The numbers aren't predictable, they are each random every time I run it. It's just confusing me as when I put a smaller number I get more sets doubling the number I need I get the correct amount. I can provide the project files if that helps make anything easier?
I won't be able to go through your project but someone else might.
Why not try isolating where the problem happens? For instance, start with just taking out the instantiation line and see how many waypoint values you get. If they are incorrect, you can zero in on the problem. If they are correct, you can start looking at the instantiation as the source of the problem.
I've been over and over your algorithm. Nothing jumps out at me.
It can't hurt to swap in a different prefab or to try a different instantiate operation.
Answer by lmoro · Jan 16, 2018 at 02:04 PM
There's nothing wrong in your script.
Maybe you added the component twice?
Here's a package with a basic implementation of your script
https://drive.google.com/open?id=1fFe7jvi-SkufkVlBt-tgNTgHy4uevc23
I believe there was a bug in Unity itself. After removing the script from the gameobject (I checked each game object and made sure the script was only added once), it still ran and had issues. Then, after actually removing the script from the project it still ran. I ended up just retrying multiple times after Unity crashed several times and it seems to be working fine now.
Your answer
Follow this Question
Related Questions
How to spawn randomly with multiple gameobject 2 Answers
Spawn gameObject horde, modify concentration of spawned objects. 1 Answer
How to spawn power ups at random places on random time internvals 1 Answer
Spawn enemies so they aren't instantiated on top of each other (C#) 2 Answers
How can I make an object clone in the time that I indicate it to appear? 0 Answers