Search Unity

Line-Renderer makes Problems

Discussion in 'Scripting' started by DubStepMaster, Dec 11, 2013.

  1. DubStepMaster

    DubStepMaster

    Joined:
    Jul 17, 2012
    Posts:
    40
    Hey there

    I want to make a script, which connects Cubes if there is on char in a string in both cubes the same.

    This is my idea:

    -Make for each object a line-renderer an give them the position of the other cube, so they connect....

    This is my script:

    Code (csharp):
    1. function Verbinden(){  
    2.  
    3.     for(item25 in SynapsenARR){
    4.         var script11 = item25.GetComponent(LineRenderer);
    5.         Destroy (script11);
    6.         script11 = item25.gameObject.AddComponent(LineRenderer);
    7.     }
    8.    
    9.        
    10.     for(item1 in SynapsenARR){ 
    11.         var zahl1 : Synapse = item1.GetComponent("Synapse");
    12.         var counter : int = 0; 
    13.         for(item2 in SynapsenARR){         
    14.             var script1 : Synapse = item1.GetComponent("Synapse");
    15.             var vergleich1 : String = script1.Zahl;
    16.             var script2 : Synapse = item2.GetComponent("Synapse");
    17.             var vergleich2 : String = script2.Zahl;
    18.             if(vergleich1.Length == vergleich2.Length){
    19.                 if(item1 != item2){
    20.                     for (var k = 0; k<=(AnzahlDerStellen-1);k++) {
    21.                     Debug.Log(k.ToString());               
    22.                         if(vergleich1[k] == vergleich2[k]){                    
    23.                             var script10 = item1.GetComponent(LineRenderer);                       
    24.                             if (script10 == null)
    25.                             {
    26.                                     script10 = item1.gameObject.AddComponent(LineRenderer);
    27.                                     script10.material = new Material (Shader.Find("Particles/Additive"));
    28.                                     script10.SetColors( Color.red,  Color.red);
    29.                             }
    30.                            
    31.                            
    32.                             counter += 2;
    33.                             script10.SetWidth(0.05, 0.05);
    34.                             script10.SetVertexCount(counter+1);
    35.                             script10.SetPosition(counter-2, item1.transform.position);
    36.                             script10.SetPosition(counter-1, item2.transform.position);
    37.                         }
    38.                     }
    39.                 }
    40.             }
    41.         }
    42.     }
    43. }
    SynapsenARR is a List
    in Synapse is the string Zahl which should compare.

    The Problem is, that when there coming more than 4 Cubes, this script doesn´t works anymore.

    The lines jumping around, though their have to be the same.

    Where is my mistake?

    Thank you for helping me.
     
  2. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Not sure about your problem, but numbers in variables can be a good thing and could lead to errors in the future.

    If your destroying the instance at line 5 then your prob not assigning it at line 6.
     
  3. DubStepMaster

    DubStepMaster

    Joined:
    Jul 17, 2012
    Posts:
    40
    thanks for your help :)

    the problem was the VertexCount wasn´t high enough
     
  4. DubStepMaster

    DubStepMaster

    Joined:
    Jul 17, 2012
    Posts:
    40
    i solve the script so:

    Code (csharp):
    1. function Verbinden(){  
    2.  
    3.     for(item25 in SynapsenARR){
    4.         var script11 = item25.GetComponent(LineRenderer);
    5.         if (script11 == null)
    6.         {
    7.                 script11 = item25.gameObject.AddComponent(LineRenderer);
    8.                 script11.material = new Material (Shader.Find("Particles/Additive"));
    9.                 script11.SetColors( Color.red,  Color.red);
    10.         }
    11.         script11.SetVertexCount(AnzahlDerStellen*SynapsenARR.Count*10);
    12.         for (var l = 0; l<=(AnzahlDerStellen*SynapsenARR.Count*10-1);l++) {
    13.             script11.SetPosition(l, transform.position);
    14.             Debug.Log(l.ToString());
    15.         }
    16.     }
    17.    
    18.        
    19.     for(item1 in SynapsenARR){ 
    20.         var zahl1 : Synapse = item1.GetComponent("Synapse");
    21.         var counter : int = 0; 
    22.         for(item2 in SynapsenARR){         
    23.             var script1 : Synapse = item1.GetComponent("Synapse");
    24.             var vergleich1 : String = script1.Zahl;
    25.             var script2 : Synapse = item2.GetComponent("Synapse");
    26.             var vergleich2 : String = script2.Zahl;
    27.             if(vergleich1.Length == vergleich2.Length){
    28.                 if(item1 != item2){
    29.                     for (var k = 0; k<=(AnzahlDerStellen-1);k++) {             
    30.                         if(vergleich1[k] == vergleich2[k]){                    
    31.                             var script10 = item1.GetComponent(LineRenderer);                       
    32.                             if (script10 == null)
    33.                             {
    34.                                     script10 = item1.gameObject.AddComponent(LineRenderer);
    35.                                     script10.material = new Material (Shader.Find("Particles/Additive"));
    36.                                     script10.SetColors( Color.red,  Color.red);
    37.                             }
    38.                            
    39.                            
    40.                             counter += 2;
    41.                             script10.SetWidth(0.05, 0.05);
    42.                             script10.SetVertexCount(counter+1);
    43.                             script10.SetPosition(counter-2, item1.transform.position);
    44.                             script10.SetPosition(counter-1, item2.transform.position);
    45.                         }
    46.                     }
    47.                 }
    48.             }
    49.         }
    50.     }
    51. }
    the problem is now, it doesn´t works right:

    here are two pictures that shows the renderresult with a box in front with the string Zahl as name.

    $block1.PNG $block2.PNG

    my question is now:

    Why did the script connects them?

    please help me...