create same (but randomly positioned) prefab combination multiple times in start()
Hi, I am new to unity and for my current project I would like to have 4 prefabs positioned randomly on 4 different positions multiple times (4 prefabs = one gameobject). I got 2 seperate pieces of code working, but so far I have not been able to combine it into one piece of code succesfully. The first piece of the code randomly assigns a prefab to a position, but only once. The second piece initiates a prefab the amount of times set in the barPoolsize and is working with one simple prefab. I think I need to change the "firstprefab" to the result of the "spawnBlocks()" in order to make the code work the way I want to, which I believe would mean that I need to turn the result of "spawnBlocks()" into a GameObject. What would be the best way to do this? Or is there another/better solution for what I want to achieve? Any help will be appreciated!
// positions blocks
public Vector3 blockpos1 = new Vector3(-1.5f, 3.5f, 0);
public Vector3 blockpos2 = new Vector3(-4.5f, 3.5f, 0);
public Vector3 blockpos3 = new Vector3(1.5f, 3.5f, 0);
public Vector3 blockpos4 = new Vector3(4.5f, 3.5f, 0);
public List<Vector3> possiblePositionsBlock;
public GameObject firstprefab;
public GameObject secondprefab;
public GameObject thirdprefab;
public GameObject fourthprefab;
public List<GameObject> itemBlocks;
public int barPoolSize = 5;
private GameObject[] bars;
void Start ()
{
//add to list
possiblePositionsBlock.Add(blockpos1);
possiblePositionsBlock.Add(blockpos2);
possiblePositionsBlock.Add(blockpos3);
possiblePositionsBlock.Add(blockpos4);
itemBlocks.Add(firstprefab);
itemBlocks.Add(secondprefab);
itemBlocks.Add(thirdprefab);
itemBlocks.Add(fourthprefab);
bars = new GameObject[barPoolSize];
for (int k = 0; k < barPoolSize; k++)
{
spawnBlocks();
bars[k] = (GameObject)Instantiate(firstprefab, startPosition, Quaternion.identity);
}
}
public void spawnBlocks()
{
for (int i = 0; i < possiblePositionsBlock.Count; i++) // as long as positions are not filled
{
int j = Random.Range(0, itemBlocks.Count); // select random block
Instantiate(itemBlocks[j], possiblePositionsBlock[i], Quaternion.identity); // position block
itemBlocks.RemoveAt(j); // remove block from list to avoid double
}
}
Answer by Chris333 · Apr 05, 2019 at 01:20 PM
What i understood: You have 4 Positions and 4 Prefabs. You want all 4 prefabs instantiated but always on different positions.
Edited code, see comment
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
public Vector3 blockpos1 = new Vector3(-1.5f, 3.5f, 0);
public Vector3 blockpos2 = new Vector3(-4.5f, 3.5f, 0);
public Vector3 blockpos3 = new Vector3(1.5f, 3.5f, 0);
public Vector3 blockpos4 = new Vector3(4.5f, 3.5f, 0);
public List<Vector3> possiblePositionsBlock;
public GameObject firstprefab;
public GameObject secondprefab;
public GameObject thirdprefab;
public GameObject fourthprefab;
public List<GameObject> itemBlocks;
private List<GameObject> groupsOfBars;
void Start()
{
//add to list
possiblePositionsBlock.Add(blockpos1);
possiblePositionsBlock.Add(blockpos2);
possiblePositionsBlock.Add(blockpos3);
possiblePositionsBlock.Add(blockpos4);
groupsOfBars = new List<GameObject>();
spawnBlocks();
}
public void spawnBlocks()
{
int maxSpawnCount = possiblePositionsBlock.Count;
itemBlocks.Add(firstprefab);
itemBlocks.Add(secondprefab);
itemBlocks.Add(thirdprefab);
itemBlocks.Add(fourthprefab);
GameObject newGroupOfBars = new GameObject()
{
name = "Group of Bars"
};
for (int i = 0; i < maxSpawnCount; i++) // as long as positions are not filled
{
int randomBlock = Random.Range(0, itemBlocks.Count); // select random block
GameObject block = Instantiate(itemBlocks[randomBlock], possiblePositionsBlock[i], Quaternion.identity) as GameObject; // position block
block.transform.parent = newGroupOfBars.transform;
itemBlocks.RemoveAt(randomBlock); // remove block from list to avoid double
}
groupsOfBars.Add(newGroupOfBars);
}
class GroupOfBars
{
public List<GameObject> bars { get; set; }
public GroupOfBars()
{
bars = new List<GameObject>();
}
}
}
Hi @Chris333 , thank you for your response. That is indeed what I want, but I want to be able to instantiate these 4 prefabs multiple times at random positions and then use each set of 4 individually in the game. In your code you add each block to the list "bars". Would it be possible to move 4 entries in this list to a new list as a single item? If so, I could maybe empty the "bars" list after 4 items (and refill until desired amount of combinations) and call the new list whenever I need the combination of 4 prefabs.
I changed the code so that every time you call spawnBlocks() it creates an empty gameObject and attaches the 4 blocks as children to it.
Your answer
Follow this Question
Related Questions
Save gameobject from imported mesh (with imported texture) as prefab/resource at runtime. 0 Answers
What is Casting in Unity ? 1 Answer
Create gameobject in hierarchy on Unity refresh but before Play mode 0 Answers
How to instantiate Prefabs for an endless game? 0 Answers
Getting the gameobject in the map 0 Answers