I'm trying to create a script where I can spawn different type of objects
Asked
Active
Viewed 500 times
1
-
1Do you want to guarantee the 2 of 10 objects to be one type or are you looking for pure chance? I'd probably implement this as an enumerator that returned the object type/instantiated object – Charleh Jun 27 '16 at 20:22
-
i want to guarantee the the object No.2 will be spawned even if there is only one of it. i just need to be get spawned. because it has a special abilities and object no.1 is just normal. – John Jun 27 '16 at 20:23
-
You probably want to add the objects to a list in the ratio that you need, I'd pick a ratio, say 2:8 and then an amount that's a multiple such as 10, 20 etc, add the number of items to a list in the ratio specified then pick items from the list at random, removing an item when it's been picked – Charleh Jun 27 '16 at 20:42
-
I don't understand what your first `if` block is for. I think i have a solution but I don't know what you are doing in the first `if` so I am not shure how to translate it. For example, if `randomFloat` had a value of `0.95f` it should will spawn any item at all. Is that what you want or is that a bug? – Scott Chamberlain Jun 27 '16 at 20:45
-
@ScottChamberlain in unity inspector i pass my variables. let's say i pass normal object to be 0.8 (80%) and other object 0.2 (20%) the first if statement fixes the bug that i used to get when my randomFloat value gets 1 therefore it will spawn nothing in that case and i made it randomFloat <0.9 and > 0.7 because i was trying to get higher percentage of the normal create spawn.. i hope this clears it up a little bit – John Jun 27 '16 at 20:49
-
Why pass 2 persentages? Could you ever have normal object 50% other object 20% so you would end up with 30% unused? – Scott Chamberlain Jun 27 '16 at 20:50
-
@ScottChamberlain well currently in my unity inspector i have 80% and 20% but when it spawns sometimes it spawns only 3 or 4 objects of the same type. although i have it set to 6 objects to spawn. i just want it to spawn 6 objects and to have minimum of 1 special object and maximum of 2 and im trying to keep it general as possible because maybe later i want to change how many obstacles i want to spawn. i always want it to be 80% by 20% – John Jun 27 '16 at 20:53
-
Spawn tag needs to be removed; it's for spawning child processes as per tag description, but suggested edit got rejected – Pimgd Jul 01 '16 at 14:00
3 Answers
1
From your description and comments what it sounds like is you want to guarantee that between speiclaCratePercentageMin
and speiclaCratePercentageMax
percentage of crates are the special creates but the rest can just be a normal create. If that is so all you need to do is figure out how many crates that percentage will be of the total, spawn that many first, then fill the rest in with normal crates.
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
public class spawnmanager : MonoBehaviour {
public int noOfobjects = 6;
public Transform[] spawnPoints;
public GameObject normalCrate;
public GameObject specialCrate;
public float speiclaCratePercentageMin;
public float speiclaCratePercentageMax;
void Awake()
{
}
// Use this for initialization
void Start ()
{
spawner();
}
void spawner ()
{
List<Transform> availablePoints = new List<Transform>(spawnPoints);
//Figures out how many special creates we need.
int numberOfSpecialCrates = noOfobjects * Random.Range(this.speiclaCratePercentageMin, this.speiclaCratePercentageMax);
//Added i<spawnPoints.Length check to prevent errors when noOfobjects is bigger than the number of available spawn points.
for (int i = 0; i<noOfobjects && i<spawnPoints.Length;i++)
{
int spawnPointIndex = Random.Range (0, availablePoints.Count);
//As long as i is lower than numberOfSpecialCrates we spawn a special crate.
if(i < numberOfSpecialCrates)
{
Debug.Log("dd");
Instantiate(specialCrate, availablePoints[spawnPointIndex].position, Quaternion.identity);
}
else
{
Instantiate(normalCrate, availablePoints[spawnPointIndex].position, Quaternion.identity) ;
}
availablePoints.RemoveAt(spawnPointIndex);
}
}
}

Scott Chamberlain
- 124,994
- 33
- 282
- 431
-
Great! it fixed the problem :) now how can i keep track of the obstacles if they are destroyed? for example if there is only 2 obstacles on the scene i want to spawn more if you know please share your info with me if not Thanks alot it works :) – John Jun 27 '16 at 21:08
-
@Groude tag all the objects with the same tag then use the function to [search for objects in a tag](https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html), when you have too few spawn more. – Scott Chamberlain Jun 27 '16 at 21:28
-
You can also use a list that stores all instances of objects. (Or two, one for each if this makes it easier to check how many of the rare ones are currently existing.) – Gunnar B. Jun 27 '16 at 23:11
-
@ScottChamberlain Hello again, im still stuck at the respawning thing when i dont have enough obstacles on the scene. I want to do that whenever i destroy an obstacle i respawn another obstacle after 1 second. now here im thinking of using coroutines but im not really sure how the logic will be. I have 2 classes one is the manager that handles the spawning and one is the main class of the obstacle that has the functionality of the obstacle. i want to destroy it the obstacle class. – John Jun 28 '16 at 11:34
-
@Groude you really should be asking new questions for this stuff. Write up a new full question with a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and ask it there. – Scott Chamberlain Jun 28 '16 at 13:36
0
i think the problem is here if (randomFloat >=1 || randomFloat <=0.9f && randomFloat >= 0.7f)
better use more brackets
if (randomFloat >=1 || (randomFloat <=0.9f && randomFloat >= 0.7f))

amin
- 1
- 1
-
It makes it better but somehow it still sometimes spawn 4 or 5 obstacles instead of spawning 6 – John Jun 27 '16 at 20:39
-
your code is based on random and its by chance you may get around 600 of 1000 but not 6 of ten – amin Jun 27 '16 at 20:47
-
random in terms of positions but the type im trying to do in terms of percentages – John Jun 27 '16 at 20:50
0
Check out this SO question for ideas. It seems to be what you are looking for.
-
Link only answers are discouraged, even to other SO questions. Either the question is different enough this question deserves it's own complete version of the answer or else they are similar enough that you should have voted to close it as a duplicate. – Scott Chamberlain Jun 27 '16 at 20:49