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

Line connection berween 2 game objects of the same prefab

Discussion in 'Getting Started' started by bea02wil, Feb 20, 2020.

?

How can i update connection line of each draggable pair?

  1. I dont know what to type here

  2. I dont know what to type here

Results are only viewable after voting.
  1. bea02wil

    bea02wil

    Joined:
    Dec 22, 2018
    Posts:
    1
    Hello there, programmers! I'm new to Unity. So, here's my problem: I'm using a LineRenderer component to create a line. I want to connect 2 objects of the same prefab using a line and move one of them including the line. Here's my approach: In a class of my prefab object (triangle) i have a function that allows me to select an (triangle) object and if you select 2 of them with ctrl key held down then these objects are added to a List<> of my another class (selectedObjects) and then between these selected objects a line is created and you can move one of objects together with the line like it should be (1, 1.1) (according to my LineRenderer script). But the problem is that if you come back to some before created object's pair and drag one of pair's object you will only move an object without the connection line (2, 2.1). I know that my problem is in MoveLine method in setPositions of newSelectedObjectsList list because when a new line is created the list clears itself (a). So, how can i update connection line of each draggable pair? And yeah, if you know to connect objects using shape - line not using LineRenderer, please let me know. Also, this a playground of a real project where i try to fix it... This question is for Unity2D if its important.
    Here's my important part of code:

    Code (CSharp):
    1. public class ConnectionLine : MonoBehaviour
    2. {
    3.     [SerializeField] GameObject prefabOfLine;
    4.     SelectedObjects selectedObjects;  
    5.     public GameObject newLine;
    6.     public bool triangleMoves;    
    7.     public LineRenderer lineRenderer;
    8.     public List<GameObject> newSelectedObjectsList; // list for adding selectedObjects because in triangle class in some method when you realese ctrl key triangleObjects list clears itself.
    9.      
    10.     void Update()
    11.     {
    12.          if (selectedObjects.redObjectsCount == 2) //If 2 objects are selected then create a line
    13.          {          
    14.              CreateConnectionLine();
    15.              selectedObjects.redObjectsCount = 0;
    16.          }
    17.        
    18.         if (triangleMoves) //turns into true when you drag a triangle (implemented in triangle script)
    19.         {          
    20.             MoveLine();        
    21.         }
    22.     }
    23.  
    24.     private void CreateConnectionLine() //function that creates a connection between 2 selected objects
    25.     {
    26.         newLine = Instantiate(prefabOfLine, Vector2.zero, Quaternion.identity);
    27.         lineRenderer = newLine.GetComponent<LineRenderer>();
    28.         newSelectedObjectsList.Clear(); //  ([B]a[/B])
    29.         newSelectedObjectsList.Add(selectedObjects.triangleObjects[0]); // triangleObjects - list where selected objects  are added.
    30.         newSelectedObjectsList.Add(selectedObjects.triangleObjects[1]);    
    31.         lineRenderer.SetPosition(0, newSelectedObjectsList[0].transform.position);
    32.         lineRenderer.SetPosition(1, newSelectedObjectsList[1].transform.position);  
    33.     }
    34.  
    35.     private void MoveLine()
    36.     {
    37.         lineRenderer.SetPosition(0, newSelectedObjectsList[0].transform.position); // ([B]a)[/B]
    38.         lineRenderer.SetPosition(1, newSelectedObjectsList[1].transform.position);  // (      
    39.     }  
    40. }
    image.png
     

    Attached Files: