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 Wuzseen · Jun 27, 2012 at 08:40 PM · instantiateruntimecrashes

Crashing While Instantiating at Runtime

This question is slightly disjointed from one I asked yesterday, but is an extension in a couple ways.

http://answers.unity3d.com/questions/275061/instantiating-custom-prefabs.html

I've got some spheres being instantiated randomly about the map. I only do it for the first 200 frames via manipulating update. I have a gameobject that initializes a set amount of spheres randomly about the map. Here's that code:

 var spherePick : Transform;
 private var count : int;
 count = 0;
 
 function Update ()
 {
     if( count <= 200 )
     {
         var A = Random.Range(0.0, 500.0);
         var B = Random.Range(0.0, 500.0);
         var C = Random.Range(0.0, 500.0);
            Instantiate(spherePick, Vector3 (A, B, C), Quaternion.identity );
             count++;
     }
  }

The problem comes from attempting to use similar code on my first person controller player character. When the PC picks up a sphere I want more to spawn randomly on the map. Keep in mind this is all using the same prefab. So, I have the following function and variable inside my script that handles the pickups.

 var spherePick : Transform;
 
 function create ( amount : int )
 {
     var count : int = 0;
     while ( count <= amount )
     {
         var A = Random.Range(0.0, 500.0);
         var B = Random.Range(0.0, 500.0);
         var C = Random.Range(0.0, 500.0);
         Instantiate(spherePick, Vector3 (A, B, C), Quaternion.identity );
     }
     Debug.Log("New Spheres Spawned!");
 }

So the problem is, whenever my PC tries to pick up (triggered inside OnControllerColliderHit via recognizing collision with a the prefab's tag) a sphere, it crashes when trying to generate more. That is to say, the Update() script generates the spheres properly, and the spheres can be picked up. However, adding in the create() function now crashes unity whenever I try to pick up a sphere

Not really sure how or why this is going on. I saw some stuff suggesting that maybe my transformed vars needed to be private. I tried this and didn't have any luck. Not sure how I can go about debugging an issue like this either. Something I've been wondering is if I have my parameter in create defined correctly. What's there in the code is simply the latest thing I've tried. I've tried create( int amount ) create( amount ) create( int : amount) all to no avail.

EDIT: Seeing some formatting issues with the first chunk of code... looks OK in preview and it isn't fixing itself, I apologize for that.

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 Mizuho · Jun 27, 2012 at 08:45 PM 0
Share

Is this possibly because you have an infinite loop happening with the spawning? So it's trying to spawn an infinite amount of spheres because OnControllerColliderHit keeps firing (what if spheres spawn on you)?

avatar image Wuzseen · Jun 27, 2012 at 08:51 PM 0
Share

While not exactly what you were saying, I just noticed I don' increment count. That does cause an infinite loop... which is bad.

avatar image Wuzseen · Jun 27, 2012 at 08:53 PM 0
Share

And that was the solution. Dios mios, can't believe I missed that. Well, that's a good chunk of time down the drain.

avatar image Mizuho · Jun 27, 2012 at 08:57 PM 0
Share

Game freezing usually goes hand in hand with infinite loops (every time I've frozen my game, it was because of an infinite loop). If you ever freeze again, check for infinite loops! ...Also, I didn't even read your code lol. Just looked at the title and posted this :D.

1 Reply

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

Answer by Wuzseen · Jun 27, 2012 at 09:20 PM

Missed an infinite loop I was causing in the second bit of code. Derp!

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

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

Problem with instantiate in runtime 2 Answers

Avoid altering prefab at runtime 1 Answer

Create new scene at runtime 1 Answer

Export objects to a .3DS file at runtime 1 Answer

Instantiate and edit a text box during runtime 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