Adding a dot within bounds of sprite

I just started using unity and I want to add a single dot to a game object.
I tried using line renderer with a code from online:

void Update()
{
if (Input.GetMouseButtonDown(0)){
CreateDot();
}
}

    void CreateDot()
    {
        currentLine = Instantiate(linePrefab, Vector3.zero, Quaternion.identity);
        lineRenderer = currentLine.GetComponent<LineRenderer>();
        fingerPositions.Clear();
        fingerPositions.Add(Camera.main.ScreenToWorldPoint(Input.mousePosition));
        fingerPositions.Add(Camera.main.ScreenToWorldPoint(Input.mousePosition));
        lineRenderer.SetPosition(0, fingerPositions[0]);
        lineRenderer.SetPosition(1, fingerPositions[1]);
    }

This creates a dot but single fixed position.

What I need is to create a dot within bounds of another game object where mouse is clicked.
Thanks for any help.

Code example for a raycast to hit your gameobject, and to then create the dot at this position:

void Update(){
  if(Input.GetMouseButtonDown(0)){
    Ray ray = Camera.main.ScreenPointToRay(Input.MousePosition);
    if(Physics.Raycast(ray, out RaycastHit hit) && hit.gameObject == YourGameObject){
      currentLine = Instantiate(linePrefab, hit.point, Quaternion.identity);
    }
  }
}

You check, whether the mouse is pressed, then shoot a raycast from the current mouse position and direction of the camera into the scene. If it hits something and this something is also your gameobject, you create a point at the resulting coordinates. Otherwise, it does nothing :slight_smile:

For more information on raycasts, have a look at this tutorial.

Standard browser disclaimer applies.