Change the active value quickly?
Ok so I'm making my game have all different maps in one scene and I imagine that having like 10 HUUUGE terrains would affect my performance right?
Well I have this setup:
and this function to load the maps, but changing the active state on this many objects is causing a 3 minute lag spike.
var maps : Transform[];
static var loadedMap : int;
var terrain : Terrain;
function LoadMap (index : int){
loadedMap = index;
sunGO.transform.position = maps[index].position;
for(var i=0; i<maps.length; i++){
if(i==index){
sunGO.transform.position = maps[i].position;
GameObject.Find("CloudDome").transform.position.x = maps[i].position.x;
GameObject.Find("CloudDome").transform.position.z = maps[i].position.z;
maps[i].gameObject.active = true;
terrain = maps[i].Find("Terrain").GetComponent.<Terrain>();
}else{
maps[i].gameObject.active = false;
}
}
}
Now I should give a little background on what this function does.
The "terrain" variable is simply what my walking sound script uses to check what terrain I'm on and then get the texture I'm on, nothing related to my question.
"CloudDome" is that thing you see in the distance on the main map.
"sunGO" is the GameObject that holds the sun, moon, weather, and other ambient things.
The "maps" variable is what holds the Tranform parents of the maps, see it here:
Now if it froze just when loading a map, that would be fine, but it freezes as soon as I host a server because it's setting all of the other maps (only 2 maps at the time) to be inactive.
I apologize for this being so long, please help me fix this or at least come up with a workaround.
Thanks in advance!
Answer by DroidifyDevs · May 29, 2016 at 06:50 PM
I don't see why you have that many terrains in one scene. Why not just make separate scenes for each map? If you're loading many maps at a time of course you will have a lag spike. Regardless on whether or not your maps are active or inactive, at the start of the game it will still load all the objects, reducing performance and increasing load time. To make the load time shorter, you need to reduce the amount of stuff to load, so make the maps in separate scenes.
It's complicated, the way everything is set up is for that scene, creating another scene (which I have done before in the game) causes a lot of problems for the game, also causes problems with players joining the server from a different map, which I could fix that but there are a lot of other problems with changing maps that I cannot deal with.
When the game starts, every map is active. When you join it hits a trigger in the map that your player was in to set all other maps inactive, this is when the game freezes.
Should setting them INactive really cause a lag spike?
Also thank you for the quick reply
The lag isn't when I first start the game, that loads fine. It's when I actually click host it sets the other maps as inactive, that freezes it.
Your answer
Follow this Question
Related Questions
Car made from Different GameObjects and Empty GameObjects breaks on Collision with Terrain. 0 Answers
Fast way to texture a 10km map? 1 Answer
How to make a GUI bar that counts items collected 0 Answers
Copying or duplicating a gameObject from one scene to another 1 Answer
GameObject is passing through the mountains in the terrain. 0 Answers