Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Draw a line between 2 objects chosen in game

Discussion in 'Scripting' started by enricovicari, Apr 30, 2021.

  1. enricovicari

    enricovicari

    Joined:
    Jun 11, 2020
    Posts:
    3
    Hello,
    I'm trying to draw a line between 2 game objects.
    The problem is that I want the user to choose the 2 game objects while in game.

    My approach was:
    Put custom script on prefab parent

    on prefab Child 1:
    Activate script using SelectEntered (or Activated)

    on prefab Child2:
    Activate script using SelectEntered (or Activated)

    And the code I thought of:

    Code (CSharp):
    1. public class MakeLine2Parts : MonoBehaviour
    2. {
    3.     private LineRenderer line;
    4.     [SerializeField] Material Material;
    5.     [SerializeField] float lineWidth;
    6.     void Start()
    7.     {
    8.         // Add a Line Renderer to the GameObject
    9.         line = this.gameObject.AddComponent<LineRenderer>();
    10.         // Set the width of the Line Renderer
    11.         line.startWidth = lineWidth;
    12.         // Set the number of vertex fo the Line Renderer
    13.         line.positionCount = 2;
    14.     }
    15.  
    16.     public void ObjectSelection()
    17.     {
    18.         if (true)
    19.         {
    20.             line.SetPosition(0, this.gameObject.transform.position);
    21.         }
    22.         else
    23.         {
    24.             line.SetPosition(1, this.gameObject.transform.position);
    25.         }
    26.     }
    27. }
    However I have no idea what conditions I should set for if/else.
    gameObject != null sounded right in my head but then how do I dynamically fill that with whatever gameObjects the user choose?

    For context the environment is:
    Child1 and Child2 always in scene
    There is a button a user can press to instantiate/spawn Child(x)

    The flow would be:
    User spawns x number of Children -> selected ChildX -> select ChildY -> a line is drawn.

    I hope you'll be able to help me
    Thank you!
    (I'm new to coding and started using Unity about a month ago! Sorry if this is a stupid question )
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    You would need to feed those GameObjects that the user selects into this script, which would then take their positions out of the Transform component on the GameObject.

    Remember, .gameObject is a shortcut to the GameObject that THIS script here is located on.
     
    Joe-Censored likes this.
  3. enricovicari

    enricovicari

    Joined:
    Jun 11, 2020
    Posts:
    3
    Hey @Kurt-Dekker thanks so much for the quick reply!
    Any hint on how I'd go for "feeding the GameObjects that the user selected into this script"?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    I would make a method on the line positioning script that accepts the two new pieces of data from whatever script you are using to "pick the two pieces."

    - If they accept the position, it will be frozen in time at that moment (Vector3 is a value type)

    - if they accept the transform, they can look in the transform to get the up-to-date position at any time.

    Either approach may suit you, or it might be you want one particular way or another.

    For stuff to help you clicking on things and selecting them, look to Youtube tutorials. Remember it has to be stateful and wait until you have 2 selected to send it over.