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 /
avatar image
0
Question by twoxsu · Nov 13, 2016 at 09:03 AM · instantiaterandom.range

Random.Range when instantiating things at start freezes the game

Hey i got an problem with instantiating prefabs (trees, rocks, bushes) on startup. I have a script to do it:

using UnityEngine; using System.Collections;

 public class raySpawn : MonoBehaviour {
 public GameObject[] propy = new GameObject[30]; //Array of prefabs
 private Vector3 origin;
 private int index = 0;
 private bool czyIndexPoprawny = false; //currently unused
     // Use this for initialization
     void Start () {
         origin = gameObject.transform.position;
         RaycastHit hit;
         for(int i = 0; i < 4000; i++)
         {
             Ray linia = new Ray(gameObject.transform.position, new Vector3(Random.Range(-1000,1000), -500, Random.Range(-1000,1000)));
             if(Physics.Raycast(linia,out hit) && hit.transform.tag == "ZaczepBudowy") //If it hits terrain
             {
                 if(hit.point.y > 24) //So things wont generate at sea level
                 {
                     index = Random.Range(0,12);
                     //if(index != null)
                         Instantiate(propy[index],hit.point,Quaternion.identity);
                 }
             }
             else
                 i--;
             
             //Debug.Log(Random.Range(0,11).ToString()); I checked if its something horribly wrong with Random.Range itself
         }
     }
 }
 

Theres currently 12 objects in array (0 to 11). I dont know why, but Random.Range(X,Y) includes the X but excludes the Y, so i have to add one digit to it. When i put 10 as a Y, it works (only 9 of 12 will spawn) but when i put 11 as Y, the game freezes horribly and makes F and S in FPS switch place. (Seconds per Frame ¯_(ツ)_/¯ ). I seek an solution for this. I checked if one prefab of those is causing a massive lag, but i found that not one of the prefabs is causing the problem. Unaccounted takes up to 95% of memory in the profiler. I dont know why

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Raresh · Nov 13, 2016 at 09:05 AM

The documentation says clearly, that Random.Range is max inclusive only for FLOATS. https://docs.unity3d.com/ScriptReference/Random.Range.html

Comment
Add comment · 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
0

Answer by tanoshimi · Nov 13, 2016 at 10:42 AM

Your solution to your infinite loop is to remove the following line, which causes the for loop counter to not increment:

 else
   i--;
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 twoxsu · Nov 13, 2016 at 05:06 PM 0
Share

It executes only if Physics.Raycast AND the hit object tag != "ZaczepBudowy" fails.

avatar image tanoshimi twoxsu · Nov 13, 2016 at 05:07 PM 0
Share

Yep, and that test is failing, which is why you're getting stuck forever.

avatar image twoxsu twoxsu · Nov 13, 2016 at 05:39 PM 0
Share

It doesnt fail. If it would, it wouldnt spawn things on the map.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Why this error when trying to instantiate object ?(Solved) 2 Answers

Instantiate issues 1 Answer

Stopping Instantiated GameObjects overlapping 3 Answers

How do i prevent an object instantiating another object straight away 2 Answers

Instantiate GameObject at Random time is not working 1 Answer


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