Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Switching meshes

Discussion in 'Scripting' started by varie-tea, Jun 26, 2014.

  1. varie-tea

    varie-tea

    Joined:
    Jul 4, 2012
    Posts:
    24
    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):
    1. if(!isToggled){
    2.     if(GetComponent(MeshFilter).mesh != T)
    3.             GetComponent(MeshFilter).mesh.Clear();
    4.             GetComponent(MeshFilter).mesh = T;
    5.             GetComponent(MeshFilter).mesh.RecalculateBounds();
    6.             GetComponent(MeshFilter).mesh.RecalculateNormals();
    7.     }else{
    8.         if(GetComponent(MeshFilter).mesh != notT)
    9.             GetComponent(MeshFilter).mesh.Clear();
    10.             GetComponent(MeshFilter).mesh = notT;
    11.             GetComponent(MeshFilter).mesh.RecalculateBounds();
    12.             GetComponent(MeshFilter).mesh.RecalculateNormals();
    13.     }
    14. }
    15.  
    If anyone knows what I'm missing, any help would be greatly appreciated!

    Thanks!
     
  2. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    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.
     
  3. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    Code (CSharp):
    1. MeshFilter mf = GetComponent<MeshFilter>();
    2.         if(isToggled && mf.sharedMesh != T){
    3.             mf.mesh = T;
    4.         }else if(!isToggled && mf.sharedMesh != notT){
    5.             mf.mesh = notT;
    6.         }
     
    Fraconte and Magiichan like this.
  4. varie-tea

    varie-tea

    Joined:
    Jul 4, 2012
    Posts:
    24
    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!
     
  5. varie-tea

    varie-tea

    Joined:
    Jul 4, 2012
    Posts:
    24
    Thanks, but what is your script doing differently than mine?
     
  6. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    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.
     
  7. varie-tea

    varie-tea

    Joined:
    Jul 4, 2012
    Posts:
    24
    Thanks, I've tried using your code but it still freezes/crashes.
     
  8. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    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?
     
  9. varie-tea

    varie-tea

    Joined:
    Jul 4, 2012
    Posts:
    24
    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!
     
  10. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    I'm not sure it will help.
    My last idea for you (before I give up :p ) 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.
     
  11. varie-tea

    varie-tea

    Joined:
    Jul 4, 2012
    Posts:
    24
    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.
     
  12. varie-tea

    varie-tea

    Joined:
    Jul 4, 2012
    Posts:
    24
    I still haven't found a solution for this problem.
    Does anyone else have an idea?
    Many thanks!
     
  13. duck

    duck

    Unity Technologies

    Joined:
    Oct 21, 2008
    Posts:
    358
    I would suggest pasting the whole script. We have no real idea of the wider context, or even what "T" and "notT" is :)
     
  14. varie-tea

    varie-tea

    Joined:
    Jul 4, 2012
    Posts:
    24
    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):
    1. #pragma strict
    2.  
    3. //Script Types
    4. public var isPlayerScript : boolean = false;
    5. public var isPickupP1 : boolean = false;
    6. public var isPickupP5 : boolean = false;
    7. public var isPickupM1 : boolean = false;
    8. public var isPickupM5 : boolean = false;
    9. public var isScoreText : boolean = false;
    10. public var isDestroyTimeScript : boolean = false;
    11. public var isDoorOpen : boolean = false;
    12. public var isDoorOpenStay : boolean = false;
    13. public var isDoorOpenStayC : boolean = false;
    14. public var isExit : boolean = false;
    15. public var isButton2M : boolean = false;
    16. public var isTurntable : boolean = false;
    17. public var isTurntableEmply : boolean = false;
    18.  
    19. public var isMenuMove : boolean = false;
    20. public var isMenuL1 : boolean = false;
    21. public var isMenuL2 : boolean = false;
    22. public var isMenuL3 : boolean = false;
    23. public var isMenuL4 : boolean = false;
    24. public var isMenuL5 : boolean = false;
    25. public var isMenuL6 : boolean = false;
    26. public var isMenuL7 : boolean = false;
    27. public var isMenuL8 : boolean = false;
    28. public var isMenuL9 : boolean = false;
    29. public var isMenuReset : boolean = false;
    30. public var isQuit : boolean = false;
    31.  
    32.  
    33.  
    34. //Buttons/Toggles
    35. public var toggle : boolean = false;
    36. public var isToggled : boolean = false;
    37. public var notT : Mesh;
    38. public var T : Mesh;
    39.  
    40.  
    41.  
    42. //Level vars
    43. public var inLevel1 : boolean = false;
    44. public var inLevel2 : boolean = false;
    45. public var inLevel3 : boolean = false;
    46. public var inLevel4 : boolean = false;
    47. public var inLevel5 : boolean = false;
    48. public var inLevel6 : boolean = false;
    49. public var inLevel7 : boolean = false;
    50. public var inLevel8 : boolean = false;
    51. public var inLevel9 : boolean = false;
    52.  
    53.  
    54.  
    55. //Top Scores
    56. public var L1TopScore : int = 8;
    57. public var L2TopScore : int = 45;
    58. public var L3TopScore : int = 18;
    59. public var L4TopScore : int = 23;
    60. public var L5TopScore : int = 23;
    61. public var L6TopScore : int = 23;
    62. public var L7TopScore : int = 23;
    63. public var L8TopScore : int = 23;
    64. public var L9TopScore : int = 23;
    65.  
    66.  
    67. //color
    68. public var menuTextMain : Color;
    69. public var menuTextNA : Color;
    70.  
    71.  
    72.  
    73. //Transforms & GameObjets
    74. public var pickupSound : Transform;
    75. public var doorSound : Transform;
    76. public var buttonSound : Transform;
    77. public var gameButtonSound : Transform;
    78. public var pickupMain : GameObject;
    79. public var doorGraphics : GameObject;
    80. public var menuCam : GameObject;
    81. public var buttonGameObject : GameObject;
    82.  
    83.  
    84.  
    85. //Movement
    86. public var verticalForce : float = 25;
    87. public var horizontalForce : float = 25;
    88. public var jumpForce : float = 25;
    89. public var airTimes : float = 0.2;
    90. public var jumpRayLength : float = 1.5; //was 1.1
    91. public var moveRayLength : float = 1.2; //was 1.1
    92.  
    93.  
    94.  
    95. //Misc
    96. static var score : int = 0;
    97. public var destroyTimer : float = 1.0;
    98. public var anim : Animator;
    99. public var levelToLoad : String;
    100. public var levelToUnlock : String = "L0";
    101. public var menuMove : Vector3;
    102. public var doorStayOpenBoolean : boolean = true;
    103. public var messageToSend1 : String;
    104. public var messageToSend1Input : float;
    105. public var messageToSend2 : String;
    106. public var messageToSend2Input : float;
    107. public var messageToSend3 : String;
    108. public var messageToSend3Input : float;
    109. public var messageToSend4 : String;
    110. public var messageToSend4Input : float;
    111. public var messageToSend5 : String;
    112. public var messageToSend5Input : float;
    113. public var turntableSpeed : float = 0.0;
    114. public var td : boolean = false;
    115. public var tdd : float = 10;
    116. public var s : float;
    117.  
    118. function Start () {
    119.     if(isPlayerScript)
    120.         score = 0;
    121.    
    122.     s = turntableSpeed;
    123.    
    124.     if(isDestroyTimeScript){
    125.         Destroy(gameObject, destroyTimer);
    126.     }
    127. }
    128.  
    129. function Update () {
    130.     if(isButton2M){
    131.        
    132.         if(!isToggled){
    133.         //This caused the game to crash!!!
    134.             if(GetComponent(MeshFilter).sharedMesh != T)
    135.                 GetComponent(MeshFilter).mesh.Clear();
    136.                 GetComponent(MeshFilter).mesh = T;
    137.                 GetComponent(MeshFilter).mesh.RecalculateBounds();
    138.                 GetComponent(MeshFilter).mesh.RecalculateNormals();
    139.         }else{
    140.             if(GetComponent(MeshFilter).sharedMesh != notT)
    141.                 GetComponent(MeshFilter).mesh.Clear();
    142.                 GetComponent(MeshFilter).mesh = notT;
    143.                 GetComponent(MeshFilter).mesh.RecalculateBounds();
    144.                 GetComponent(MeshFilter).mesh.RecalculateNormals();
    145.         }
    146.         //!!
    147.     }
    148.    
    149.     if(isDoorOpenStayC){
    150.         if(!GameObject.Find("Pickup +1 Main T")){
    151.             Destroy(gameObject);
    152.         }
    153.     }
    154.  
    155.     if(isTurntableEmply) {
    156.         if(!GameObject.Find("Pickup +1 Main T")) {
    157.             turntableSpeed = 0;
    158.         }
    159.     }
    160.  
    161.     if(isScoreText){
    162.         if(inLevel1)
    163.             GetComponent(TextMesh).text = "Score: " + score + "/" + L1TopScore;
    164.         if(inLevel2)
    165.             GetComponent(TextMesh).text = "Score: " + score + "/" + L2TopScore;
    166.         if(inLevel3)
    167.             GetComponent(TextMesh).text = "Score: " + score + "/" + L3TopScore;
    168.         if(inLevel4)
    169.             GetComponent(TextMesh).text = "Score: " + score + "/" + L4TopScore;
    170.         if(inLevel5)
    171.             GetComponent(TextMesh).text = "Score: " + score + "/" + L5TopScore;
    172.         if(inLevel6)
    173.             GetComponent(TextMesh).text = "Score: " + score + "/" + L6TopScore;
    174.         if(inLevel7)
    175.             GetComponent(TextMesh).text = "Score: " + score + "/" + L7TopScore;
    176.         if(inLevel8)
    177.             GetComponent(TextMesh).text = "Score: " + score + "/" + L8TopScore;
    178.         if(inLevel9)
    179.             GetComponent(TextMesh).text = "Score: " + score + "/" + L9TopScore;
    180.     }
    181.    
    182.     if(isTurntable){
    183.         if(td){
    184.             if(Vector3.Distance(transform.position, GameObject.Find("Player").transform.position) > tdd){
    185.                 turntableSpeed = 0;
    186.             }else{
    187.                 turntableSpeed = s;
    188.             }
    189.         }
    190.    
    191.         if(turntableSpeed != 0){
    192.             audio.volume = 1;
    193.         }else{
    194.             audio.volume = 0;
    195.         }
    196.     }
    197.    
    198.     if(isPlayerScript){
    199.         if(Input.GetButtonDown("To Main Menu")){
    200.             Application.LoadLevel("MainMenu");
    201.         }
    202.        
    203.         if(Input.GetButtonDown("Restart Level")){
    204.             Application.LoadLevel(Application.loadedLevel);
    205.         }
    206.     }
    207.    
    208.     if(Input.GetKeyDown(KeyCode.C)){
    209.         PlayerPrefs.SetInt("L2", 1);
    210.         PlayerPrefs.SetInt("L3", 1);
    211.         PlayerPrefs.SetInt("L4", 1);
    212.         PlayerPrefs.SetInt("L5", 1);
    213.         PlayerPrefs.SetInt("L6", 1);
    214.         PlayerPrefs.SetInt("L7", 1);
    215.         PlayerPrefs.SetInt("L8", 1);
    216.     }
    217.    
    218.     if(isMenuL1){
    219.         GetComponent(TextMesh).text = "L1: Getting Started (" + PlayerPrefs.GetInt("L1TS") + "/" + L1TopScore + ")";
    220.     }
    221.    
    222.     if(isMenuL2){
    223.         if(PlayerPrefs.GetInt("L2") == 1){
    224.             GetComponent(TextMesh).color = menuTextMain;
    225.         }else{
    226.             GetComponent(TextMesh).color = menuTextNA;
    227.         }
    228.        
    229.         GetComponent(TextMesh).text = "L2: Bit Harder (" + PlayerPrefs.GetInt("L2TS") + "/" + L2TopScore + ")";
    230.     }
    231.    
    232.     if(isMenuL3){
    233.             if(PlayerPrefs.GetInt("L3") == 1){
    234.                 GetComponent(TextMesh).color = menuTextMain;
    235.             }else{
    236.                 GetComponent(TextMesh).color = menuTextNA;
    237.             }
    238.            
    239.             GetComponent(TextMesh).text = "L3: First Signs of a Puzzle (" + PlayerPrefs.GetInt("L3TS") + "/" + L3TopScore + ")";
    240.     }
    241.    
    242.     if(isMenuL4){
    243.             if(PlayerPrefs.GetInt("L4") == 1){
    244.                 GetComponent(TextMesh).color = menuTextMain;
    245.             }else{
    246.                 GetComponent(TextMesh).color = menuTextNA;
    247.             }
    248.            
    249.             GetComponent(TextMesh).text = "L4: Where's the button (" + PlayerPrefs.GetInt("L4TS") + "/" + L4TopScore + ")";
    250.     }
    251.    
    252.     if(isMenuL5){
    253.             if(PlayerPrefs.GetInt("L5") == 1){
    254.                 GetComponent(TextMesh).color = menuTextMain;
    255.             }else{
    256.                 GetComponent(TextMesh).color = menuTextNA;
    257.             }
    258.            
    259.             GetComponent(TextMesh).text = "L5: Jump the gap (" + PlayerPrefs.GetInt("L5TS") + "/" + L5TopScore + ")";
    260.     }
    261.    
    262.     if(isMenuL6){
    263.             if(PlayerPrefs.GetInt("L6") == 1){
    264.                 GetComponent(TextMesh).color = menuTextMain;
    265.             }else{
    266.                 GetComponent(TextMesh).color = menuTextNA;
    267.             }
    268.            
    269.             GetComponent(TextMesh).text = "L6: Torque!? (" + PlayerPrefs.GetInt("L6TS") + "/" + L6TopScore + ")";
    270.     }
    271.    
    272.     if(isMenuL7){
    273.             if(PlayerPrefs.GetInt("L7") == 1){
    274.                 GetComponent(TextMesh).color = menuTextMain;
    275.             }else{
    276.                 GetComponent(TextMesh).color = menuTextNA;
    277.             }
    278.            
    279.             GetComponent(TextMesh).text = "L7: Turning Paths (" + PlayerPrefs.GetInt("L7TS") + "/" + L7TopScore + ")";
    280.     }
    281.    
    282.     if(isMenuL8){
    283.             if(PlayerPrefs.GetInt("L8") == 1){
    284.                 GetComponent(TextMesh).color = menuTextMain;
    285.             }else{
    286.                 GetComponent(TextMesh).color = menuTextNA;
    287.             }
    288.            
    289.             GetComponent(TextMesh).text = "L8: Turning Step (" + PlayerPrefs.GetInt("L8TS") + "/" + L8TopScore + ")";
    290.     }
    291.    
    292.     if(isMenuL9){
    293.             if(PlayerPrefs.GetInt("L1TS") == L1TopScore &&
    294.             PlayerPrefs.GetInt("L2TS") == L2TopScore &&
    295.             PlayerPrefs.GetInt("L3TS") == L3TopScore &&
    296.             PlayerPrefs.GetInt("L4TS") == L4TopScore &&
    297.             PlayerPrefs.GetInt("L5TS") == L5TopScore &&
    298.             PlayerPrefs.GetInt("L6TS") == L6TopScore &&
    299.             PlayerPrefs.GetInt("L7TS") == L7TopScore &&
    300.             PlayerPrefs.GetInt("L8TS") == L8TopScore){
    301.                 GetComponent(TextMesh).color = menuTextMain;
    302.             }else{
    303.                 GetComponent(TextMesh).color = menuTextNA;
    304.             }
    305.            
    306.             GetComponent(TextMesh).text = "Bonus : Expect the Unexpected (" + PlayerPrefs.GetInt("L9TS") + "/" + L9TopScore + ")";
    307.     }
    308. }
    309.  
    310. function FixedUpdate () {
    311.     if(isTurntable){
    312.         transform.eulerAngles.y += turntableSpeed;
    313.     }
    314. }
    315.  
    316. function OnMouseUp () {
    317.     if(isMenuMove){
    318.         MoveCam();
    319.         Instantiate(buttonSound, transform.position, transform.rotation);
    320.     }else if(isMenuL1){
    321.         Application.LoadLevel("Level1");
    322.     }else if(isMenuL2){
    323.         if(PlayerPrefs.GetInt("L2") == 1){
    324.             Application.LoadLevel("Level2");
    325.         }
    326.     }else if(isMenuL3){
    327.         if(PlayerPrefs.GetInt("L3") == 1){
    328.             Application.LoadLevel("Level3");
    329.         }
    330.     }else if(isMenuL4){
    331.         if(PlayerPrefs.GetInt("L4") == 1){
    332.             Application.LoadLevel("Level4");
    333.         }
    334.     }else if(isMenuL5){
    335.         if(PlayerPrefs.GetInt("L5") == 1){
    336.             Application.LoadLevel("Level5");
    337.         }
    338.     }else if(isMenuL6){
    339.         if(PlayerPrefs.GetInt("L6") == 1){
    340.             Application.LoadLevel("Level6");
    341.         }
    342.     }else if(isMenuL7){
    343.         if(PlayerPrefs.GetInt("L7") == 1){
    344.             Application.LoadLevel("Level7");
    345.         }
    346.     }else if(isMenuL8){
    347.         if(PlayerPrefs.GetInt("L8") == 1){
    348.             Application.LoadLevel("Level8");
    349.         }
    350.     }else if(isMenuL9){
    351.         if(PlayerPrefs.GetInt("L1TS") == L1TopScore &&
    352.             PlayerPrefs.GetInt("L2TS") == L2TopScore &&
    353.             PlayerPrefs.GetInt("L3TS") == L3TopScore &&
    354.             PlayerPrefs.GetInt("L4TS") == L4TopScore &&
    355.             PlayerPrefs.GetInt("L5TS") == L5TopScore &&
    356.             PlayerPrefs.GetInt("L6TS") == L6TopScore &&
    357.             PlayerPrefs.GetInt("L7TS") == L7TopScore &&
    358.             PlayerPrefs.GetInt("L8TS") == L8TopScore){
    359.             Application.LoadLevel("Level9");
    360.         }
    361.     }else if(isMenuReset){
    362.         PlayerPrefs.DeleteAll();
    363.         Instantiate(buttonSound, transform.position, transform.rotation);
    364.         MoveCam();
    365.     }else if(isQuit){
    366.         Application.Quit();
    367.     }
    368. }
    369.  
    370. function OnTriggerEnter (other : Collider) {
    371.     if(isPickupP1){
    372.         if(other.tag == "Player"){
    373.             Instantiate(pickupSound, transform.position, transform.rotation);
    374.             score++;
    375.             Destroy(pickupMain.gameObject);
    376.         }
    377.     }else if(isPickupP5){
    378.         if(other.tag == "Player"){
    379.             Instantiate(pickupSound, transform.position, transform.rotation);
    380.             score += 5;
    381.             Destroy(pickupMain.gameObject);
    382.         }
    383.     }else if(isPickupM1){
    384.         if(other.tag == "Player"){
    385.             Instantiate(pickupSound, transform.position, transform.rotation);
    386.             score -= 1;
    387.             Destroy(pickupMain.gameObject);
    388.         }
    389.     }else if(isPickupM5){
    390.         if(other.tag == "Player"){
    391.             Instantiate(pickupSound, transform.position, transform.rotation);
    392.             score -= 5;
    393.             Destroy(pickupMain.gameObject);
    394.         }
    395.     }else if(isDoorOpen){
    396.         if(other.tag == "Player"){
    397.             anim.SetInteger("inTrigger", 1);
    398.             audio.PlayOneShot(audio.clip, 1);
    399.         }
    400.     }else if(isDoorOpenStay){
    401.         if(other.tag == "Player" && doorStayOpenBoolean){
    402.             anim.SetInteger("inTrigger", 1);
    403.             audio.PlayOneShot(audio.clip, 1);
    404.             yield WaitForSeconds(1);
    405.             doorStayOpenBoolean = false;
    406.         }
    407.     }else if(isExit){
    408.         if(other.tag == "Player"){
    409.             if(inLevel1){
    410.                 if(PlayerPrefs.GetInt("L1TS") < score)
    411.                     PlayerPrefs.SetInt("L1TS", score);
    412.             }else if(inLevel2){
    413.                 if(PlayerPrefs.GetInt("L2TS") < score)
    414.                     PlayerPrefs.SetInt("L2TS", score);
    415.             }else if(inLevel3){
    416.                 if(PlayerPrefs.GetInt("L3TS") < score)
    417.                     PlayerPrefs.SetInt("L3TS", score);
    418.             }else if(inLevel4){
    419.                 if(PlayerPrefs.GetInt("L4TS") < score)
    420.                     PlayerPrefs.SetInt("L4TS", score);
    421.             }else if(inLevel5){
    422.                 if(PlayerPrefs.GetInt("L5TS") < score)
    423.                     PlayerPrefs.SetInt("L5TS", score);
    424.             }else if(inLevel6){
    425.                 if(PlayerPrefs.GetInt("L6TS") < score)
    426.                     PlayerPrefs.SetInt("L6TS", score);
    427.             }else if(inLevel7){
    428.                 if(PlayerPrefs.GetInt("L7TS") < score)
    429.                     PlayerPrefs.SetInt("L7TS", score);
    430.             }else if(inLevel8){
    431.                 if(PlayerPrefs.GetInt("L8TS") < score)
    432.                     PlayerPrefs.SetInt("L8TS", score);
    433.             }else if(inLevel8){
    434.                 if(PlayerPrefs.GetInt("L9TS") < score)
    435.                     PlayerPrefs.SetInt("L9TS", score);
    436.             }
    437.        
    438.             Application.LoadLevel(levelToLoad);
    439.             PlayerPrefs.SetInt(levelToUnlock, 1);
    440.         }
    441.     }else if(isButton2M){
    442.         if(other.tag == "Player"){
    443.             //if(toggle){
    444.                 isToggled =! isToggled;
    445.                 if(isToggled == true)
    446.                     buttonGameObject.gameObject.SendMessage(messageToSend1, messageToSend1Input);
    447.                     Instantiate(gameButtonSound, transform.position, transform.rotation);
    448.                 if(isToggled == false)
    449.                     buttonGameObject.gameObject.SendMessage(messageToSend2, messageToSend2Input);
    450.             //}
    451.         }
    452.     }
    453. }
    454.  
    455. function DoorOpen () {
    456.     if(isDoorOpen || isDoorOpenStay){
    457.         anim.SetInteger("inTrigger", 1);
    458.         audio.PlayOneShot(audio.clip, 1);
    459.     }
    460. }
    461.  
    462. function DoorClose () {
    463.     if(isDoorOpen){
    464.         anim.SetInteger("inTrigger", 2);
    465.         audio.PlayOneShot(audio.clip, 1);
    466.     }
    467. }
    468.  
    469. function TurnAdd (ad : float) {
    470.     turntableSpeed += ad;
    471. }
    472. function TurnStop () {
    473.     turntableSpeed = 0;
    474. }
    475.  
    476. function OnTriggerExit (other : Collider){
    477.     if(other.tag == "Player"){
    478.         if(isDoorOpen){
    479.             anim.SetInteger("inTrigger", 2);
    480.             audio.PlayOneShot(audio.clip, 1);
    481.         }
    482.        
    483.         if(isButton2M && !toggle){
    484.             isToggled =! isToggled;
    485.             if(isToggled == true)
    486.                 buttonGameObject.gameObject.SendMessage(messageToSend1);
    487.                 Instantiate(gameButtonSound, transform.position, transform.rotation);
    488.             if(isToggled == false)
    489.                 buttonGameObject.gameObject.SendMessage(messageToSend2);
    490.         }
    491.     }
    492. }
    493.  
    494. function MoveCam () {
    495.     menuCam.transform.position.x = menuMove.x + menuCam.transform.position.x;
    496.     menuCam.transform.position.y = menuMove.y + menuCam.transform.position.y;
    497.     menuCam.transform.position.z = menuMove.z + menuCam.transform.position.z;
    498. }
    (I wish I had written it in C# - now it's too big to rewrite)

    Thank you!
     
  15. Dave-Hampson

    Dave-Hampson

    Unity Technologies

    Joined:
    Jan 2, 2014
    Posts:
    150
    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?
     
  16. varie-tea

    varie-tea

    Joined:
    Jul 4, 2012
    Posts:
    24
    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!
     
  17. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    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).
     
  18. Dave-Hampson

    Dave-Hampson

    Unity Technologies

    Joined:
    Jan 2, 2014
    Posts:
    150
    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.
     
  19. varie-tea

    varie-tea

    Joined:
    Jul 4, 2012
    Posts:
    24
    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!
     
  20. varie-tea

    varie-tea

    Joined:
    Jul 4, 2012
    Posts:
    24
    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.
     
  21. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    You miss some { } (indent is not enough!) :)
    Actually you are assigning and recalculating every frame...

    Code (JavaScript):
    1. if(!isToggled)
    2. {
    3.     //This caused the game to crash!!!
    4.     if(GetComponent(MeshFilter).sharedMesh != T)
    5.     {
    6.         GetComponent(MeshFilter).mesh.Clear();
    7.         GetComponent(MeshFilter).mesh = T;
    8.         GetComponent(MeshFilter).mesh.RecalculateBounds();
    9.         GetComponent(MeshFilter).mesh.RecalculateNormals();
    10.     }
    11. }
    12. else
    13. {
    14.     if(GetComponent(MeshFilter).sharedMesh != notT)
    15.     {
    16.         GetComponent(MeshFilter).mesh.Clear();
    17.         GetComponent(MeshFilter).mesh = notT;
    18.         GetComponent(MeshFilter).mesh.RecalculateBounds();
    19.         GetComponent(MeshFilter).mesh.RecalculateNormals();
    20.     }
    21. }
    22.  
     
    David-Berger likes this.
  22. varie-tea

    varie-tea

    Joined:
    Jul 4, 2012
    Posts:
    24
    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.
     
    Fraconte likes this.