Hello, In my game I made a button that toggles when it's stepped on and it works fine. Then I made the button change the mesh (mesh filter) to a different mesh for when it is toggled and not toggled. Now when I run the game it starts to lag after a few minutes and then it freezes so I have to force quit it! I've tired clearing the mesh, recalculating the bounds, recalculating the normals, but nothing seems to have an effect. When I comment out that part of the script it works again. Code (JavaScript): if(!isToggled){ if(GetComponent(MeshFilter).mesh != T) GetComponent(MeshFilter).mesh.Clear(); GetComponent(MeshFilter).mesh = T; GetComponent(MeshFilter).mesh.RecalculateBounds(); GetComponent(MeshFilter).mesh.RecalculateNormals(); }else{ if(GetComponent(MeshFilter).mesh != notT) GetComponent(MeshFilter).mesh.Clear(); GetComponent(MeshFilter).mesh = notT; GetComponent(MeshFilter).mesh.RecalculateBounds(); GetComponent(MeshFilter).mesh.RecalculateNormals(); } } If anyone knows what I'm missing, any help would be greatly appreciated! Thanks!
Are you sure that this is causing your game to crash? Use the profiler to double check? ;o And ofcourse, make sure that your unity is updated.
Code (CSharp): MeshFilter mf = GetComponent<MeshFilter>(); if(isToggled && mf.sharedMesh != T){ mf.mesh = T; }else if(!isToggled && mf.sharedMesh != notT){ mf.mesh = notT; }
Yes I'm updated. I'm sure that this is causing that problem. The profiler says that the CPU usage is largely: physics, rendering, other, scripts. I've noticed something strange: while the game is getting lag spicks/slow the "Stats" window still says that the frame rate is about 80 - 120. Thanks!
mesh returns a clone of the mesh that was actually used, to prevent you from messing up other objects using the same mesh, so if you comparing mesh to something you need to use sharedMesh. Then your code's logic is different from what you explained with your own words.
Then problem is not with code alone. Did you try to comment this piece of code and change meshes by hand in inspector at runtime?
I just tried that, it works. Would it help if I were to post the entire script (it handles pickups, doors, menus, buttons, and some other stuff)? Thanks!
I'm not sure it will help. My last idea for you (before I give up ) is to not mess with meshes at all. For your object make 2 child objects with their own filters, renderers, colliders. One with T mesh, and other one with notT and enable/disable them, when trigger activates. I could think of something wrong with meshes or other components on your object, that is not happy with changing mesh. So keep 2 objects unchanged and just switch them on and off. Could be a simple solution if you are not in mood for searching the source of your problems for a long time.
Thanks, but I can't make 2 child objects since the script is applied on the button itself, not on a parent. And putting the script on an empty game object and then making the graphics objects as child objects (as you said) isn't an option since I don't have a prefab for the buttons, so I would need to make these adjustments to dozens of buttons. Since the buttons are connected to about 10-15 doors, 50-180 pickups, 3-10 turntables... in each level I would need to adjust and re-connect all of them and re-write almost 500 lines of code. Normally I wouldn't care, but at the moment I don't have enough time to make these adjustments.
I would suggest pasting the whole script. We have no real idea of the wider context, or even what "T" and "notT" is
I didn't post the entire script because most of it doesn't effect the buttons and it's about 500 lines long. T and notT are just mesh variables. Here's the script: Code (JavaScript): #pragma strict //Script Types public var isPlayerScript : boolean = false; public var isPickupP1 : boolean = false; public var isPickupP5 : boolean = false; public var isPickupM1 : boolean = false; public var isPickupM5 : boolean = false; public var isScoreText : boolean = false; public var isDestroyTimeScript : boolean = false; public var isDoorOpen : boolean = false; public var isDoorOpenStay : boolean = false; public var isDoorOpenStayC : boolean = false; public var isExit : boolean = false; public var isButton2M : boolean = false; public var isTurntable : boolean = false; public var isTurntableEmply : boolean = false; public var isMenuMove : boolean = false; public var isMenuL1 : boolean = false; public var isMenuL2 : boolean = false; public var isMenuL3 : boolean = false; public var isMenuL4 : boolean = false; public var isMenuL5 : boolean = false; public var isMenuL6 : boolean = false; public var isMenuL7 : boolean = false; public var isMenuL8 : boolean = false; public var isMenuL9 : boolean = false; public var isMenuReset : boolean = false; public var isQuit : boolean = false; //Buttons/Toggles public var toggle : boolean = false; public var isToggled : boolean = false; public var notT : Mesh; public var T : Mesh; //Level vars public var inLevel1 : boolean = false; public var inLevel2 : boolean = false; public var inLevel3 : boolean = false; public var inLevel4 : boolean = false; public var inLevel5 : boolean = false; public var inLevel6 : boolean = false; public var inLevel7 : boolean = false; public var inLevel8 : boolean = false; public var inLevel9 : boolean = false; //Top Scores public var L1TopScore : int = 8; public var L2TopScore : int = 45; public var L3TopScore : int = 18; public var L4TopScore : int = 23; public var L5TopScore : int = 23; public var L6TopScore : int = 23; public var L7TopScore : int = 23; public var L8TopScore : int = 23; public var L9TopScore : int = 23; //color public var menuTextMain : Color; public var menuTextNA : Color; //Transforms & GameObjets public var pickupSound : Transform; public var doorSound : Transform; public var buttonSound : Transform; public var gameButtonSound : Transform; public var pickupMain : GameObject; public var doorGraphics : GameObject; public var menuCam : GameObject; public var buttonGameObject : GameObject; //Movement public var verticalForce : float = 25; public var horizontalForce : float = 25; public var jumpForce : float = 25; public var airTimes : float = 0.2; public var jumpRayLength : float = 1.5; //was 1.1 public var moveRayLength : float = 1.2; //was 1.1 //Misc static var score : int = 0; public var destroyTimer : float = 1.0; public var anim : Animator; public var levelToLoad : String; public var levelToUnlock : String = "L0"; public var menuMove : Vector3; public var doorStayOpenBoolean : boolean = true; public var messageToSend1 : String; public var messageToSend1Input : float; public var messageToSend2 : String; public var messageToSend2Input : float; public var messageToSend3 : String; public var messageToSend3Input : float; public var messageToSend4 : String; public var messageToSend4Input : float; public var messageToSend5 : String; public var messageToSend5Input : float; public var turntableSpeed : float = 0.0; public var td : boolean = false; public var tdd : float = 10; public var s : float; function Start () { if(isPlayerScript) score = 0; s = turntableSpeed; if(isDestroyTimeScript){ Destroy(gameObject, destroyTimer); } } function Update () { if(isButton2M){ if(!isToggled){ //This caused the game to crash!!! if(GetComponent(MeshFilter).sharedMesh != T) GetComponent(MeshFilter).mesh.Clear(); GetComponent(MeshFilter).mesh = T; GetComponent(MeshFilter).mesh.RecalculateBounds(); GetComponent(MeshFilter).mesh.RecalculateNormals(); }else{ if(GetComponent(MeshFilter).sharedMesh != notT) GetComponent(MeshFilter).mesh.Clear(); GetComponent(MeshFilter).mesh = notT; GetComponent(MeshFilter).mesh.RecalculateBounds(); GetComponent(MeshFilter).mesh.RecalculateNormals(); } //!! } if(isDoorOpenStayC){ if(!GameObject.Find("Pickup +1 Main T")){ Destroy(gameObject); } } if(isTurntableEmply) { if(!GameObject.Find("Pickup +1 Main T")) { turntableSpeed = 0; } } if(isScoreText){ if(inLevel1) GetComponent(TextMesh).text = "Score: " + score + "/" + L1TopScore; if(inLevel2) GetComponent(TextMesh).text = "Score: " + score + "/" + L2TopScore; if(inLevel3) GetComponent(TextMesh).text = "Score: " + score + "/" + L3TopScore; if(inLevel4) GetComponent(TextMesh).text = "Score: " + score + "/" + L4TopScore; if(inLevel5) GetComponent(TextMesh).text = "Score: " + score + "/" + L5TopScore; if(inLevel6) GetComponent(TextMesh).text = "Score: " + score + "/" + L6TopScore; if(inLevel7) GetComponent(TextMesh).text = "Score: " + score + "/" + L7TopScore; if(inLevel8) GetComponent(TextMesh).text = "Score: " + score + "/" + L8TopScore; if(inLevel9) GetComponent(TextMesh).text = "Score: " + score + "/" + L9TopScore; } if(isTurntable){ if(td){ if(Vector3.Distance(transform.position, GameObject.Find("Player").transform.position) > tdd){ turntableSpeed = 0; }else{ turntableSpeed = s; } } if(turntableSpeed != 0){ audio.volume = 1; }else{ audio.volume = 0; } } if(isPlayerScript){ if(Input.GetButtonDown("To Main Menu")){ Application.LoadLevel("MainMenu"); } if(Input.GetButtonDown("Restart Level")){ Application.LoadLevel(Application.loadedLevel); } } if(Input.GetKeyDown(KeyCode.C)){ PlayerPrefs.SetInt("L2", 1); PlayerPrefs.SetInt("L3", 1); PlayerPrefs.SetInt("L4", 1); PlayerPrefs.SetInt("L5", 1); PlayerPrefs.SetInt("L6", 1); PlayerPrefs.SetInt("L7", 1); PlayerPrefs.SetInt("L8", 1); } if(isMenuL1){ GetComponent(TextMesh).text = "L1: Getting Started (" + PlayerPrefs.GetInt("L1TS") + "/" + L1TopScore + ")"; } if(isMenuL2){ if(PlayerPrefs.GetInt("L2") == 1){ GetComponent(TextMesh).color = menuTextMain; }else{ GetComponent(TextMesh).color = menuTextNA; } GetComponent(TextMesh).text = "L2: Bit Harder (" + PlayerPrefs.GetInt("L2TS") + "/" + L2TopScore + ")"; } if(isMenuL3){ if(PlayerPrefs.GetInt("L3") == 1){ GetComponent(TextMesh).color = menuTextMain; }else{ GetComponent(TextMesh).color = menuTextNA; } GetComponent(TextMesh).text = "L3: First Signs of a Puzzle (" + PlayerPrefs.GetInt("L3TS") + "/" + L3TopScore + ")"; } if(isMenuL4){ if(PlayerPrefs.GetInt("L4") == 1){ GetComponent(TextMesh).color = menuTextMain; }else{ GetComponent(TextMesh).color = menuTextNA; } GetComponent(TextMesh).text = "L4: Where's the button (" + PlayerPrefs.GetInt("L4TS") + "/" + L4TopScore + ")"; } if(isMenuL5){ if(PlayerPrefs.GetInt("L5") == 1){ GetComponent(TextMesh).color = menuTextMain; }else{ GetComponent(TextMesh).color = menuTextNA; } GetComponent(TextMesh).text = "L5: Jump the gap (" + PlayerPrefs.GetInt("L5TS") + "/" + L5TopScore + ")"; } if(isMenuL6){ if(PlayerPrefs.GetInt("L6") == 1){ GetComponent(TextMesh).color = menuTextMain; }else{ GetComponent(TextMesh).color = menuTextNA; } GetComponent(TextMesh).text = "L6: Torque!? (" + PlayerPrefs.GetInt("L6TS") + "/" + L6TopScore + ")"; } if(isMenuL7){ if(PlayerPrefs.GetInt("L7") == 1){ GetComponent(TextMesh).color = menuTextMain; }else{ GetComponent(TextMesh).color = menuTextNA; } GetComponent(TextMesh).text = "L7: Turning Paths (" + PlayerPrefs.GetInt("L7TS") + "/" + L7TopScore + ")"; } if(isMenuL8){ if(PlayerPrefs.GetInt("L8") == 1){ GetComponent(TextMesh).color = menuTextMain; }else{ GetComponent(TextMesh).color = menuTextNA; } GetComponent(TextMesh).text = "L8: Turning Step (" + PlayerPrefs.GetInt("L8TS") + "/" + L8TopScore + ")"; } if(isMenuL9){ if(PlayerPrefs.GetInt("L1TS") == L1TopScore && PlayerPrefs.GetInt("L2TS") == L2TopScore && PlayerPrefs.GetInt("L3TS") == L3TopScore && PlayerPrefs.GetInt("L4TS") == L4TopScore && PlayerPrefs.GetInt("L5TS") == L5TopScore && PlayerPrefs.GetInt("L6TS") == L6TopScore && PlayerPrefs.GetInt("L7TS") == L7TopScore && PlayerPrefs.GetInt("L8TS") == L8TopScore){ GetComponent(TextMesh).color = menuTextMain; }else{ GetComponent(TextMesh).color = menuTextNA; } GetComponent(TextMesh).text = "Bonus : Expect the Unexpected (" + PlayerPrefs.GetInt("L9TS") + "/" + L9TopScore + ")"; } } function FixedUpdate () { if(isTurntable){ transform.eulerAngles.y += turntableSpeed; } } function OnMouseUp () { if(isMenuMove){ MoveCam(); Instantiate(buttonSound, transform.position, transform.rotation); }else if(isMenuL1){ Application.LoadLevel("Level1"); }else if(isMenuL2){ if(PlayerPrefs.GetInt("L2") == 1){ Application.LoadLevel("Level2"); } }else if(isMenuL3){ if(PlayerPrefs.GetInt("L3") == 1){ Application.LoadLevel("Level3"); } }else if(isMenuL4){ if(PlayerPrefs.GetInt("L4") == 1){ Application.LoadLevel("Level4"); } }else if(isMenuL5){ if(PlayerPrefs.GetInt("L5") == 1){ Application.LoadLevel("Level5"); } }else if(isMenuL6){ if(PlayerPrefs.GetInt("L6") == 1){ Application.LoadLevel("Level6"); } }else if(isMenuL7){ if(PlayerPrefs.GetInt("L7") == 1){ Application.LoadLevel("Level7"); } }else if(isMenuL8){ if(PlayerPrefs.GetInt("L8") == 1){ Application.LoadLevel("Level8"); } }else if(isMenuL9){ if(PlayerPrefs.GetInt("L1TS") == L1TopScore && PlayerPrefs.GetInt("L2TS") == L2TopScore && PlayerPrefs.GetInt("L3TS") == L3TopScore && PlayerPrefs.GetInt("L4TS") == L4TopScore && PlayerPrefs.GetInt("L5TS") == L5TopScore && PlayerPrefs.GetInt("L6TS") == L6TopScore && PlayerPrefs.GetInt("L7TS") == L7TopScore && PlayerPrefs.GetInt("L8TS") == L8TopScore){ Application.LoadLevel("Level9"); } }else if(isMenuReset){ PlayerPrefs.DeleteAll(); Instantiate(buttonSound, transform.position, transform.rotation); MoveCam(); }else if(isQuit){ Application.Quit(); } } function OnTriggerEnter (other : Collider) { if(isPickupP1){ if(other.tag == "Player"){ Instantiate(pickupSound, transform.position, transform.rotation); score++; Destroy(pickupMain.gameObject); } }else if(isPickupP5){ if(other.tag == "Player"){ Instantiate(pickupSound, transform.position, transform.rotation); score += 5; Destroy(pickupMain.gameObject); } }else if(isPickupM1){ if(other.tag == "Player"){ Instantiate(pickupSound, transform.position, transform.rotation); score -= 1; Destroy(pickupMain.gameObject); } }else if(isPickupM5){ if(other.tag == "Player"){ Instantiate(pickupSound, transform.position, transform.rotation); score -= 5; Destroy(pickupMain.gameObject); } }else if(isDoorOpen){ if(other.tag == "Player"){ anim.SetInteger("inTrigger", 1); audio.PlayOneShot(audio.clip, 1); } }else if(isDoorOpenStay){ if(other.tag == "Player" && doorStayOpenBoolean){ anim.SetInteger("inTrigger", 1); audio.PlayOneShot(audio.clip, 1); yield WaitForSeconds(1); doorStayOpenBoolean = false; } }else if(isExit){ if(other.tag == "Player"){ if(inLevel1){ if(PlayerPrefs.GetInt("L1TS") < score) PlayerPrefs.SetInt("L1TS", score); }else if(inLevel2){ if(PlayerPrefs.GetInt("L2TS") < score) PlayerPrefs.SetInt("L2TS", score); }else if(inLevel3){ if(PlayerPrefs.GetInt("L3TS") < score) PlayerPrefs.SetInt("L3TS", score); }else if(inLevel4){ if(PlayerPrefs.GetInt("L4TS") < score) PlayerPrefs.SetInt("L4TS", score); }else if(inLevel5){ if(PlayerPrefs.GetInt("L5TS") < score) PlayerPrefs.SetInt("L5TS", score); }else if(inLevel6){ if(PlayerPrefs.GetInt("L6TS") < score) PlayerPrefs.SetInt("L6TS", score); }else if(inLevel7){ if(PlayerPrefs.GetInt("L7TS") < score) PlayerPrefs.SetInt("L7TS", score); }else if(inLevel8){ if(PlayerPrefs.GetInt("L8TS") < score) PlayerPrefs.SetInt("L8TS", score); }else if(inLevel8){ if(PlayerPrefs.GetInt("L9TS") < score) PlayerPrefs.SetInt("L9TS", score); } Application.LoadLevel(levelToLoad); PlayerPrefs.SetInt(levelToUnlock, 1); } }else if(isButton2M){ if(other.tag == "Player"){ //if(toggle){ isToggled =! isToggled; if(isToggled == true) buttonGameObject.gameObject.SendMessage(messageToSend1, messageToSend1Input); Instantiate(gameButtonSound, transform.position, transform.rotation); if(isToggled == false) buttonGameObject.gameObject.SendMessage(messageToSend2, messageToSend2Input); //} } } } function DoorOpen () { if(isDoorOpen || isDoorOpenStay){ anim.SetInteger("inTrigger", 1); audio.PlayOneShot(audio.clip, 1); } } function DoorClose () { if(isDoorOpen){ anim.SetInteger("inTrigger", 2); audio.PlayOneShot(audio.clip, 1); } } function TurnAdd (ad : float) { turntableSpeed += ad; } function TurnStop () { turntableSpeed = 0; } function OnTriggerExit (other : Collider){ if(other.tag == "Player"){ if(isDoorOpen){ anim.SetInteger("inTrigger", 2); audio.PlayOneShot(audio.clip, 1); } if(isButton2M && !toggle){ isToggled =! isToggled; if(isToggled == true) buttonGameObject.gameObject.SendMessage(messageToSend1); Instantiate(gameButtonSound, transform.position, transform.rotation); if(isToggled == false) buttonGameObject.gameObject.SendMessage(messageToSend2); } } } function MoveCam () { menuCam.transform.position.x = menuMove.x + menuCam.transform.position.x; menuCam.transform.position.y = menuMove.y + menuCam.transform.position.y; menuCam.transform.position.z = menuMove.z + menuCam.transform.position.z; } (I wish I had written it in C# - now it's too big to rewrite) Thank you!
That right there is what you need to dig into further. What is it about changing the mesh which causes it to lag? What if you wire up the mesh toggle to a button press, does it still happen? If you remove the mesh instead of replacing it, does it still happen? If you edit the mesh in some way, does it still happen What is lagging exactly? Does something have high CPU Is there a piece of code which is being executed repeatedly?
Hello, Thank you for your answer. I've done what you've asked me to do, here are the results: 1. I don't know what's causing it to lag and freeze, I only know that when I comment out that piece of code it doesn't lag and freeze. 2. When I wire it up with a KeyCode.<key> it doesn't lag and freeze. 3. When I remove the mesh instead of changing it, it also doesn't lag and freeze. 4. Editing the mesh didn't change anything, it still lags and freezes. 5. I don't know what exactly is causing the lag, the profiler doesn't show any change in FPS. Just to clarify: the problem occurs on its own, even if I just enter playmode and don't do anything else. I really hope you can help!! Many thanks!
Not that I can help but I was wondering if this is the same sort of thing I'd need to do if I want a character that changes "faces" based on if he is near an enemy or not (for instance a happy/sad/angry face that switch based on different conditions).
Ok so, your next step should be figuring why there is that difference: why is it that pressing a key does not cause that behaviour, yet using isToggled in an if statement does cause that behaviour. Is isToggled true when you think it is true? As an example you could try next, what happens if you swap the if (isToggled) to be if (isToggled && keydown etc) ? What happens then? It's all just a process of trying to find where the bug lies really.
Hello Dave, thanks for your answer. Yes, isToggled it true when it should be true and is false when it should be false, I've checked it using Debug.Log. No it doesn't freeze/crash when I use "isToggled && Input.GetKeyDown(KeyCode.<key>)", I've left it running for 30 mins, usually it freezes/crashes after about 15 mins. Hopefully this information will help. Thanks again!
Dear Dave, above I tried to answer your questions. Do you have an idea how this issue can be fixed? It's really urgent. Many thanks.
You miss some { } (indent is not enough!) Actually you are assigning and recalculating every frame... Code (JavaScript): if(!isToggled) { //This caused the game to crash!!! if(GetComponent(MeshFilter).sharedMesh != T) { GetComponent(MeshFilter).mesh.Clear(); GetComponent(MeshFilter).mesh = T; GetComponent(MeshFilter).mesh.RecalculateBounds(); GetComponent(MeshFilter).mesh.RecalculateNormals(); } } else { if(GetComponent(MeshFilter).sharedMesh != notT) { GetComponent(MeshFilter).mesh.Clear(); GetComponent(MeshFilter).mesh = notT; GetComponent(MeshFilter).mesh.RecalculateBounds(); GetComponent(MeshFilter).mesh.RecalculateNormals(); } }
Thank you Fraconte, I had overlooked the requirement of { } here! I've corrected this, and now my game is running fine and already on the Mac App Store. Many thanks again.