Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Allietzuh · Apr 05, 2019 at 11:44 AM · gameobjectprefab

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         
         }   
     }


  
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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>();
         }
     }
 }


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Allietzuh · Apr 08, 2019 at 11:41 AM 0
Share

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.

avatar image Chris333 Allietzuh · Apr 09, 2019 at 08:51 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

233 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges