- Home /
Question by
Hyde-Sevilen · Jun 28, 2019 at 09:13 AM ·
instantiatelooprandom.rangeparent-childspawning problems
How Instantiate Object Inside of Object and Instantiate Parent Object On Looping.
Hello guys I have problem on my Instantiate object. My goal here is to create fish on group of fishes, first of all i want to spawn fish with random amount for one group of fishes.
So every group has different amount of fish, but i have a problem, while create a spawn fish the random number is same for all group of fishes.
so how to adjust this script to achieve my goal. thanks in advance.
void Start()
{
SpawnFish();
SpawnAllFishes();
}
void SpawnFish()
{
int randomSpawnFish = Random.Range(spawnMinFishAmount, spawnMaxFishAmount);
Debug.Log("Random Number Show : " + randomSpawnFish);
for (int i = 0; i < randomSpawnFish; i++)
{
var position = new Vector3(Random.Range(-0.3f, 0.3f), 0, Random.Range(-0.3f, 0.3f));
var fishSpawner = Instantiate(fishObject, position, Quaternion.identity);
fishSpawner.transform.SetParent(fishGroup.transform, false);
}
}
public void SpawnAllFishes()
{
for (int i = 1; i <= fishSpawned; i++)
{
var position = new Vector3(Random.Range(-50f, 50f), 0, Random.Range(-55f, 15f));
Instantiate(fishGroup, position, Quaternion.identity);
Debug.Log("Fish in Group : "+fishGroup.transform.childCount);
}
}
Comment
I think you have to call the SpawnFish() inside the SpawnAllFishes() for loop.
Your answer
Follow this Question
Related Questions
that items doesnt spawn in player position 1 Answer
Instantiate issues 1 Answer
Why this error when trying to instantiate object ?(Solved) 2 Answers
Looping through multiple lists 0 Answers
Spawn an object to a random spawn point from a list 2 Answers