Search Unity

problem with the script freezing

Discussion in 'Scripting' started by carbondudeoxide, Mar 29, 2013.

  1. carbondudeoxide

    carbondudeoxide

    Joined:
    Mar 24, 2013
    Posts:
    21
    hello i have this script that i am building but unity freezes when it calls for the DestinationChanged() coroutine

    the game starts fine when you press play (to test it) but as soon as you change the Destination value the game freezes.

    what could be causing this?
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class stargate : MonoBehaviour {
    7.    
    8.     //operation variables
    9.     public string Destination = "";
    10.     string oldDes = "";
    11.     public bool Active = false;
    12.     public string Address = "";
    13.     public string Address9 = "";
    14.     public string PointOfOrigin = "";
    15.     public string NetworkNo = "";
    16.    
    17.     //sub-operation variables
    18.     string incoming_key = "!";
    19.     string activation_key = "~";
    20.     int minInputLength = 7;
    21.     int maxInputLength = 9;
    22.    
    23.     public int incoming_dialmode = 1;
    24.     public int main_dialmode = 1;
    25.    
    26.    
    27.     int disty = 1;
    28.     bool debounce = false;
    29.     bool dialActive = false;
    30.     bool inuse = false;
    31.     bool fail = false;
    32.     int NumInput = 0;
    33.     int TotalInput = 0;
    34.     private int dir = 0;
    35.     string[] DialingBuffer = new string[0];
    36.     //Queue<string> DialBuff = new Queue<string>();
    37.     List<string> DialBuff = new List<string>();
    38.    
    39.     //object variables
    40.     private GameObject ring;
    41.    
    42.     private GameObject chevron7;
    43.     private GameObject chevron7F;
    44.     private GameObject chevron7M;
    45.     private GameObject chevron7L;
    46.    
    47.     private GameObject chevronX;
    48.     private GameObject chevronXF;
    49.     private GameObject chevronXM;
    50.     private GameObject chevronXL;
    51.    
    52.     //color variables
    53.     private Color chevoff = new Color(0.412f,0.158f,0.040f);
    54.     private Color chevon = new Color(0.882f,0.341f,0.086f);
    55.    
    56.     void Start () {
    57.         chevron7 = GameObject.Find("Chevron7");
    58.         chevron7F = GameObject.Find("Chevron7Frame");
    59.         chevron7M = GameObject.Find("Chevron7Move");
    60.         chevron7L = GameObject.Find("Chevron7Light");
    61.        
    62.         ring = GameObject.Find("InnerRing");
    63.     }
    64.     // Update is called once per frame
    65.     void FixedUpdate() {
    66.         ring.transform.Rotate(Vector3.forward, 0.6f * dir);
    67.     }
    68.     void Update () {
    69.         if ((Destination != oldDes)  (!inuse)) {
    70.             oldDes = Destination;
    71.             StartCoroutine(DestinationChanged());
    72.         }
    73.        
    74.         /*
    75.         if (Active) {ActivateS();} else {DeactivateS();}
    76.         if (!debounce) {
    77.             StartCoroutine(LockChevron());
    78.         }*/
    79.     }
    80.    
    81.     private void chevron(bool on,string num) {
    82.         if (num=="") {
    83.             for(int i=1;i<10;i++) {
    84.                 chevronX = GameObject.Find("Chevron"+i);
    85.                 chevronXF = GameObject.Find("Chevron"+i+"Frame");
    86.                 chevronXM = GameObject.Find("Chevron"+i+"Move");
    87.                 chevronXL = GameObject.Find("Chevron"+i+"Light");
    88.                 if (chevronX  chevronXL) {
    89.                     if (on) {
    90.                         chevronX.renderer.material.color = chevon;
    91.                         chevronXL.renderer.material.color = chevon;
    92.                     }
    93.                     else {
    94.                         chevronX.renderer.material.color = chevoff;
    95.                         chevronXL.renderer.material.color = chevoff;
    96.                     }
    97.                 }
    98.                        
    99.             }
    100.         }
    101.         else {
    102.             chevronX = GameObject.Find("Chevron"+num);
    103.             chevronXF = GameObject.Find("Chevron"+num+"Frame");
    104.             chevronXM = GameObject.Find("Chevron"+num+"Move");
    105.             chevronXL = GameObject.Find("Chevron"+num+"Light");
    106.             if (chevronX  chevronXL) {
    107.                 if (on) {
    108.                     chevronX.renderer.material.color = chevon;
    109.                     chevronXL.renderer.material.color = chevon;
    110.                 }
    111.                 else {
    112.                     chevronX.renderer.material.color = chevoff;
    113.                     chevronXL.renderer.material.color = chevoff;
    114.                 }
    115.             }
    116.         }
    117.     }
    118.     //lock chevron with no arguments
    119.     private IEnumerator LockChevron(){
    120.         debounce = true;
    121.         chevron7F.audio.Play();
    122.         for(int i = 0; i < 25; i++) {
    123.             chevron7M.transform.Translate(-Vector3.up * 0.003f);
    124.             chevron7L.transform.Translate(-Vector3.up * 0.003f);
    125.             yield return new WaitForSeconds(0.009f);
    126.         }
    127.         for(int i = 0; i < 25; i++) {
    128.             chevron7.transform.Translate(Vector3.up * 0.002f);
    129.             yield return new WaitForSeconds(0.009f);
    130.         }
    131.         chevron(true,"7");
    132.         yield return new WaitForSeconds(0.125f);
    133.         for(int i = 0; i < 25; i++) {
    134.             chevron7M.transform.Translate(Vector3.up * 0.003f);
    135.             chevron7L.transform.Translate(Vector3.up * 0.003f);
    136.             chevron7.transform.Translate(-Vector3.up * 0.002f);
    137.             yield return new WaitForSeconds(0.009f);
    138.         }
    139.         chevron(false,"7");
    140.         yield return new WaitForSeconds(1f);
    141.         chevron7F.audio.Stop();
    142.         debounce = false;
    143.     }
    144.     //on changed
    145.     private bool keepDialing() {   
    146.         if ((Active) || (DialBuff.ToArray().Length == 0) || (Destination == "") || (NumInput > 9)) {
    147.             return false;
    148.         }
    149.         // prevents gate from dialing if gate is active, has nothing to dial, or has already activated every chevron
    150.         return true;
    151.     }
    152.     //dialy stuff
    153.     private IEnumerator dialGate(int mode) {
    154.         if (!Active || !dialActive) {
    155.             dialActive = true;
    156.            
    157.             if (mode == 1) {
    158.                 do {
    159.                     NumInput++;
    160.                     chevron (true,""+NumInput+"");
    161.                     //DialBuff.RemoveAt(0);
    162.                 }
    163.                 while (keepDialing());
    164.             }
    165.             else if (mode == 2) {
    166.                 do {
    167.                     NumInput++;
    168.                     dir = 1;
    169.                     chevron (true,""+NumInput+"");
    170.                     //DialBuff.RemoveAt(0);
    171.                 }
    172.                 while (keepDialing());
    173.             }
    174.             else if (mode == 3) {
    175.                
    176.             }
    177.            
    178.             yield return new WaitForSeconds(0.1f);
    179.             dialActive = false;    
    180.         }
    181.     }
    182.     private IEnumerator DestinationChanged(){
    183.         inuse = true;
    184.         if (!Active) {
    185.             if (Destination=="") {DeactivateS();}
    186.             else {
    187.                 oldDes=Destination;
    188.        
    189.                 string inputString = Destination;
    190.                 string[] t = inputString.Split(","[0]);
    191.                 int n = t.Length;
    192.                
    193.             //  if (tlength==0) {Destination="";}
    194.        
    195.                    
    196.                 for(int i = 0; i < n; i++) {
    197.                     if (i > TotalInput) {
    198.                         DialBuff.Add(t[i]);
    199.                         TotalInput++;
    200.                     }
    201.                 }
    202.                
    203.                 string finalChar = t[t.Length-1];
    204.                
    205.                 if (finalChar == incoming_key) {
    206.                     chevron(false,"");
    207.                     StartCoroutine(dialGate(incoming_dialmode));
    208.                 }
    209.                 else {
    210.                     StartCoroutine(dialGate(main_dialmode));
    211.                 }
    212.                
    213.                 if (finalChar == activation_key) {
    214.                    
    215.                     string s = "";
    216.                     if (n < minInputLength) {s = "short";} else if (n > maxInputLength) {s = "long";} else {s = "";}
    217.                    
    218.                     if (s!="") {Debug.LogError("Stargate: Input address too "+s+"."); fail=true; Destination="";}
    219.                    
    220.                     while (DialBuff.Count > 0) {}
    221.                     if (Destination != "") {
    222.                    
    223.                         int cutoff = 0;
    224.                        
    225.                         string inputOrigin = "";
    226.                         if ((PointOfOrigin != "")  (n < maxInputLength)) {
    227.                             inputOrigin = t[n];
    228.                             cutoff++;
    229.                         }
    230.                        
    231.                         string inputNetwork = NetworkNo;
    232.                         if ((NetworkNo != "")  (n > minInputLength)  (n < maxInputLength)) {
    233.                             if (inputOrigin=="") {inputNetwork=t[n];} else {inputNetwork=t[n-1];}
    234.                             cutoff++;
    235.                         }
    236.                        
    237.                         string inputAddress = t[1];
    238.                         for(int i=2; i<(n-cutoff+1); i++) {
    239.                             inputAddress = inputAddress+","+t[i];
    240.                         }
    241.                         /*
    242.                         string connected = "";
    243.                        
    244.                         if ((connected == "") || (Active) || (inputOrigin != PointOfOrigin)) {
    245.                             yield return new WaitForSeconds(1.0f);
    246.                             Debug.Log ("Stargate: connection failed");
    247.                             Debug.Log ("Input: Address = \""+inputAddress+"\",  Point of Origin = \""+inputOrigin+"\",  Network = \""+inputNetwork+"\"");
    248.                             fail = true;
    249.                             DeactivateS();
    250.                         }
    251.                         else {
    252.                             Debug.Log ("Stargate: connection successful");
    253.                             //connect()
    254.                         }
    255.                            
    256.                            
    257.                         /*
    258.     local connected = (self.DialingMode.Value == 0) and self or self:findByAddress(inputAddress,inputNetwork,true,(n == maxInputLength))
    259.  
    260.     if (connected == nil) or (connected:isActive()) or (inputOrigin ~= self.PointOfOrigin.Value) then
    261.         wait(1)
    262.         print("Stargate: connection failed")
    263.         print("Input:  Address = \""..inputAddress.."\",  Point of Origin = \""..inputOrigin.."\",  Network = \""..inputNetwork.."\"")
    264.         fail = true
    265.         self:deactivate()
    266.     else
    267.         print("Stargate: connection successful")
    268.         self:connectTo(connected)
    269.     end
    270.                         */
    271.                        
    272.                     }
    273.                 }
    274.             }
    275.         }
    276.         else { Debug.Log ("hello");}
    277.        
    278.         Debug.Log ("time to connect to a different world :D");
    279.        
    280.         yield return new WaitForSeconds(0.1f);
    281.         inuse = false;
    282.  
    283.     }
    284.     public void ActivateS() {
    285.     }
    286.     public void DeactivateS() {
    287.         Debug.Log("meeps");
    288.     }
    289. }
    290.  
    291.  
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You're potentially starting a new coroutine every frame that can potentially start more new coroutines which can potentially start another coroutine that has a while loop that does several GameObject.Find calls every iteration.
     
  3. Kinos141

    Kinos141

    Joined:
    Jun 22, 2011
    Posts:
    969
    do you have a loop in a loop?
     
  4. carbondudeoxide

    carbondudeoxide

    Joined:
    Mar 24, 2013
    Posts:
    21
    yes, shoudl i not?