When an object is clicked, a line spawning from the player goes to the object

Im trying to make a 2d game where theres a player who sends a rope when someone clicks on an object.
Im new and can’t think of a way to do this other than the first part which I don’t know if is even correct.

if (Input.GetMouseButtonDown(0)){

}

Im also thinking of using
if (Input.GetMouseButtonUp(0)){
}

And drawing a line between the points where the person clicks the object and releases the mouse from the object.

Please help…

Your idea is the right one. Now you need to attach a LineRenderer component to the player GameObject, and then, when the rope should be drawn, access this component and assign it’s values depending on the target position (the object that is being clicked on, or the mouse position in World coordinates).

So first, you have to add the LineRenderer to the player. You need to give a material to the LineRenderer so it can be visible, and set it’S start and end width. Then, put this script on your player:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

	private LineRenderer lineRenderer;

	// Use this for initialization
	void Start () {
		lineRenderer = GetComponent<LineRenderer>();
		lineRenderer.enabled = false;
	}
	
	// Update is called once per frame
	void Update () {

		Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
		mousePos.y = transform.position.y;//this ensures that the end of the rope is at the level of the player.
		//otherwise it would be close to the camera.

		if (Input.GetMouseButton(0)){

			Vector3 startPos = transform.position;
			Vector3 endPos = mousePos;

			lineRenderer.SetPosition(0, startPos);
			lineRenderer.SetPosition(1, endPos);
			lineRenderer.enabled = true;
		}
		if (Input.GetMouseButtonUp(0)){
			lineRenderer.enabled = false;
		}
	}
}

This should show a line from the player to to mouse cursor as long as the Left mouse button is pressed.