- Home /
Reassigning an Array causing problems
Hey guys. I've been working with Unity for a few months now. I've never had to ask a question on here before, because the documentation is great. But I'm just stumped with this one. I would really appreciate the help.
I have an array (myArray). I also have a function (myFunction) that returns an array of Vector3 positions. I want to set myArray equal to the array that myFunction returns. This works perfectly fine... the first time. Then, when I try to change myArray later on, Unity crashes and gives the error message "Fatal Error in Garbage Collector: Too many heap sections". Here is some simple example code...
var myArray : Array = new Array();
myArray = myFunction();
var position : Vector3 = myArray[0];
This works fine. Position is assigned a Vector3, and all is well. Then, when I try to reassign myArray (by doing myArray = myFunction();) some time afterwards, I get the error message. There is nothing wrong with my function. I can call it as much as I want, and assign it to other arrays. But the second I try to reassign an array, I get this error. I also tried this, with no luck....
myArray = new Array();
myArray = myFunction();
Could someone please help me? I've been working on this project all night. I've nearly completed it, but this is the only part that has stumped me.
I am seeing this error quite often these days "Fatal Error in Garbage Collector: Too many heap sections", is there a possibility that you're using 4.0b7 ?
Yeah I was planning on switching to lists or binary heaps. It's for a nav$$anonymous$$esh pathfinding system I'm making. I just wanted to get it working first before I make it more complicated. All of the pathfinding works. The character navigates his way to the waypoint. But as soon as I store a new path in the array, it crashes. And no, I'm using 3.5.2f2
I'm sure lists are better to use... but I don't think that's the problem. I just did a test where I calculated 10 paths to 10 random locations every frame in an update function. Still got well over 200 frames per second. And no garbage collector error. I dont think the problem is co$$anonymous$$g from my pathfinding function. It only happens when I reassign an array... I don't understand why
What is the function? And even if you really would not want to use List as suggested you should at least use the built-in arrays.
Arrays do not change size. Use list. If you are trying to extend your array you are getting out of bounds and that is dangerous to the application. Use list, it is like array but flexible, Edit: yes Js Array are sizeable.
Answer by janzdott · Aug 26, 2012 at 06:41 PM
It took hours of debugging, and I found that there was a problem with my pathfinding function. There was an infinite loop, but it only occured when calculating a new path. I fixed it and my pathfinding is fully functional now. This is being designed for an Android game, so its very simplistic and easy to use. I will hopefully post my work soon, after some optimization of course :)
Your answer
Follow this Question
Related Questions
Spaceship game, Pick weapon 1 Answer
Storing functions in a list/array? 1 Answer
Array problem? help please! 1 Answer
Add a temporary variable to an array 1 Answer
Array Problem - Error Code BCE0022 1 Answer