- Home /
This script really makes my game lag!
var grassBlock : Transform;
function Update () {
for(var grassLayerX = -5; grassLayerX < 5; grassLayerX++)
for(var grassLayerZ = -5; grassLayerZ < 5; grassLayerZ++)
{
grassBlock = Instantiate(grassBlock, Vector3 (grassLayerX, 0,grassLayerZ)Quaternion.identity);
}
}
The original script had the grass layer variables at -500.
This script is supossed to generate a flat grass terrain from a prefab I made. But with the original it won't open. I tried compiling it and it would just come up with an errror screen I've never seen before saying : ERROR failed to initialize the player Could not preload global game manager #0. I think it's saying something about the build settings and level 0 but I did set the level to 0 in the build settings. Then I tried making it -5 in the script above and it works but it's SUPER laggy. The thing is it's not my computer because I just got a brand new one and it's has a really good processor and stuff all the newest parts and all other really HD games work smoothly.
So what's wrong is it because of the generation? Will it get better FPS after a minute or 2? Please help!
Answer by rabbitfang · Dec 28, 2011 at 04:58 AM
The reason it is so laggy is that it is creating 100 or so objects every frame. From looking at your code, you want to change Update()
to Start()
.
P.S. I guess that the reason that the -500 value didn't work was because that would generate about 250,000 (500^2) objects.
EDIT: Also, it seems that you are trying to recreate Minecraft in Unity (based on your other questions). This is not a very good idea. Unity was not designed to do something like that, at least in the way you are going about it.
I realized that I was using update not too long after I typed his up and submitted it. But still untiy wont create a lot of blocks :(
There are only so many objects that Unity can handle. If you are doing a 500 by 500 grid, that is 250,000. This is not a good way of going about making $$anonymous$$inecraft.
Not really. You need to go about recreating $$anonymous$$inecraft a different way.
$$anonymous$$inecraft works with chunks of - I believe - 32*32 block columns, and loads a few of these. The rest is saved in the savefile. They probably also have very efficient rendering and memory handling algorithms. Which are probably very hard to recreate in Unity.