- Home /
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.
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)?
While not exactly what you were saying, I just noticed I don' increment count. That does cause an infinite loop... which is bad.
And that was the solution. Dios mios, can't believe I missed that. Well, that's a good chunk of time down the drain.
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.
Answer by Wuzseen · Jun 27, 2012 at 09:20 PM
Missed an infinite loop I was causing in the second bit of code. Derp!
Your answer
![](https://koobas.hobune.stream/wayback/20220613073403im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem with instantiate in runtime 2 Answers
Avoid altering prefab at runtime 1 Answer
Create new scene at runtime 1 Answer