Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by theopittz · Mar 09, 2021 at 08:51 PM · prefabrandominstantiate prefab

Hi , whats a way i can instantiate a prefab list randomly but make sure at least one of the prefabs is a certain chosen one?

alt text

Here is what i've got at the moment. This is used to instantiate a wall of object but i want at least one of them to always be a gap which could be prefab 7 for example.,alt text

code-stuff.png (21.6 kB)
Comment
Add comment · Show 4
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 $$anonymous$$ · Mar 10, 2021 at 01:37 AM 0
Share

Just use an Array and assign the gameObjects in the inspector

No need to have references to each prefab and then adding to a list.

avatar image KoenigX3 · Mar 10, 2021 at 11:35 AM 0
Share

Could you be more specific? I don't quite understand the situation.

If you want to randomly instantiate, then your approach is good. How many objects do you want to instantiate? Could you provide an example?

Also, as @$$anonymous$$ mentioned, you should really use an array of GameObjects.

 public GameObject[] prefabs; // This can be assigned through the inspector
 
 int prefabIndex = UnityEngine.Random.Range(0, prefabs.Length - 1);
 Instantiate(prefabs[prefabIndex], transform.position, Quaternion.identity);
avatar image theopittz · Mar 10, 2021 at 06:50 PM 0
Share

alt text

alt text

To be more specific i have a row of spawn points where the prefabs are instantiated from individually. I used to have certain patterns that were randomly chosen from but i decided to make it that each spawn point randomized the prefab it chooses individually. So back to the question, is there a way i can make it so that in that row of 5 prefabs spawned how can i make it so that there is always at least one gap in the wall. Thankyou

code-3.png (12.3 kB)
code-2.png (16.7 kB)
avatar image theopittz theopittz · Mar 10, 2021 at 06:51 PM 0
Share

@KoenigX3 ^

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KoenigX3 · Mar 10, 2021 at 07:46 PM

In this case, you need a script that controls all of the instantiating.

If you are using one global script that instantiates at these spawn points, then you should make a list of the indices that were chosen randomly, then check if it contains the index of the 'gap', and if it does not, you can select one of the spawn points randomly to generate a gap.

     public Transform[] spawnPoints;
     public GameObject[] prefabs;
 
     const int gapIndex = 0;
 
     void Start()
     {
         List<int> usedIndices = new List<int>();
         for (int i = 0; i < spawnPoints.Length; i++) usedIndices.Add(UnityEngine.Random.Range(0, prefabs.Length));
 
         if (!usedIndices.Contains(gapIndex)) usedIndices[UnityEngine.Random.Range(0, usedIndices.Count)] = gapIndex;
 
         for(int i = 0; i < spawnPoints.Length; i++) Instantiate(prefabs[usedIndices[i]], spawnPoints[i].position, Quaternion.identity);
     }

Note: my mistake, you don't have to use Count - 1, because the second parameter is exclusive.

Comment
Add comment · Show 1 · 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 theopittz · Mar 10, 2021 at 08:11 PM 0
Share

@KoenigX3 worked exactly as i wanted thankyou very much!

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

155 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

Related Questions

How can I make my prefab fire out at a random velocity every 2 secs 2 Answers

Animator not animating a runtime instantiated child 0 Answers

Random.Range not working. 1 Answer

How to randomly generate prefab? 2 Answers

How to spawn Prefab to a certain position. 2 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