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 Razputin · Feb 17, 2014 at 05:16 AM · instantiateprefabrandomvalue

Random.value sometimes doesn't instantiate my prefab(pic included)

I know its because i'm using the values wrong, but i've been playing with them for a while and haven't been able to find the correct ones.

using UnityEngine; using System.Collections; public class CubeGrid : MonoBehaviour { public Transform grassLand; public Transform dirtRoad; public Transform city; public int xCubes; public int yCubes; private int x = 0; private int y = 0; void Start () { while(y < yCubes) { if(x == xCubes) { y++; x = 0; } while(x < xCubes) { if(Random.value <= .8) { Instantiate(grassLand, new Vector3(x, y, 0), Quaternion.identity); } if(Random.value <= 0.15) { Instantiate(dirtRoad, new Vector3(x, y, 0), Quaternion.identity); } if(Random.value <= 0.05) { Instantiate(city, new Vector3(x, y, 0), Quaternion.identity); } { x++; } } } } } alt text

If somoene could tell me how to properly set up the percentages i'd be thankful.

wtfcomeonman.png (126.0 kB)
Comment
Add comment · Show 2
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 flamy · Feb 17, 2014 at 05:57 AM 0
Share

your logic is very wrong actually... if the random value is less than 0.05 you are instantiating 3 objects, is this wat u intend to do?

also the reason for some of the spots to be empty is because the value is greater than or equal to 0.8.

avatar image Razputin · Feb 17, 2014 at 06:02 AM 0
Share

I intend to instantiate more than 3 eventually actually. I know my values are already wrong I'm just not sure how to do it. The unity doc page for random.value is very small.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by AlucardJay · Feb 17, 2014 at 06:03 AM

It's because you have no conditional for when Random.value is greater than 0.8

Also with your current conditionals, you are instantiating more than one brick sometimes eg : if Random.value < 0.05, you are instantiating all 3 land types. It would be better to start checking at the lowest value, and use else if statements.

Also, Random.value is not carried through all your conditionals, rather a new value is created for each conditional.

I'm curious as to why you are using while loops instead of for loops.

Here is an example combining all my observations :

 for ( int y = 0; y < yCubes; y ++ )
 {
     for ( int x = 0; x < xCubes; x ++ )
     {
         float rnd = Random.value;
         
         if ( rnd < 0.05 )
         {
             Instantiate(city, new Vector3(x, y, 0), Quaternion.identity);
         }
         else if ( rnd < 0.15 )
         {
             Instantiate(dirtRoad, new Vector3(x, y, 0), Quaternion.identity);
         }
         else // the previous 2 conditions were not met, so just instantiate grass
         {
             Instantiate(grassLand, new Vector3(x, y, 0), Quaternion.identity);
         }
     }
 }
Comment
Add comment · Show 3 · 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 Razputin · Feb 17, 2014 at 06:09 AM 0
Share

Thanks man this is what i was trying to do but way more efficent and actually functional.

avatar image AlucardJay · Feb 17, 2014 at 06:10 AM 0
Share

Just saw the comments added while typing this. The above method works for extra tiles, just follow the same pattern :

 if ( rnd < 0.05 )
 {
     Instantiate ...
 }
 else if ( rnd < 0.15 )
 {
     Instantiate ...
 }
 else if ( rnd < 0.25 )
 {
     Instantiate ...
 }
 else if ( rnd < 0.45 )
 {
     Instantiate ...
 }
 else if ( rnd < 0.65 )
 {
     Instantiate ...
 }
 else // the previous conditions were not met, so just instantiate grass
 {
     Instantiate ...
 }
avatar image Razputin · Feb 17, 2014 at 06:13 AM 0
Share

Thanks a ton man! :D

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

20 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

Related Questions

Instantiate a random prefab at an objects location 3 Answers

Randomly pick then create prefab 2 Answers

How to access property of a prefab before Instantiating. 1 Answer

Instantiate issues 1 Answer

Prefab Instantiate 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