'Making LineRenderer Clickable with Raycast in Unity3d

I'm currently working on a project where I'm generating trees/plants with a line renderer and the l-system. Now I want to be able to click on the different plants to update their fitness score.

I tried to bake a mesh and use a mesh collider but it didn't work.

What would be the best way to approach this? Thank you in advance.

This is the part where I'm using the LineRenderer to generate the plants:

public PlantDNA Generate(Bounds bounds)
    {
        Tree = Instantiate(treeParent);
        currentString = axiom;
        StringBuilder sb = new StringBuilder();
        //L-System 
        for (int i = 0; i < iterations; i++)
        {
            foreach (var c in currentString)
            {
                // adds value onto string builder
                sb.Append(rules.ContainsKey(c) ? rules[c] : c.ToString());
            }
            currentString = sb.ToString();
            sb = new StringBuilder();
            //Debug.Log(currentString);
        }

        for (int i = 0; i < currentString.Length; i++)
        {
            switch (currentString[i])
            {
                case 'F':
                    Vector3 initialPosition = transform.position;
                    transform.Translate(Vector3.up * 2 * length);

                    GameObject treeSegment = currentString[(i + 1) % currentString.Length] == 'X' || currentString[(i + 3) % currentString.Length] == 'F' && currentString[(i + 4) % currentString.Length] == 'X' ? Instantiate(leaf) : Instantiate(branch);
                    treeSegment.transform.SetParent(Tree.transform);
                    treeSegment.GetComponent<LineRenderer>().SetPosition(0, initialPosition);
                    treeSegment.GetComponent<LineRenderer>().SetPosition(1, transform.position);
                    treeSegment.GetComponent<LineRenderer>().startWidth = width;
                    treeSegment.GetComponent<LineRenderer>().endWidth = width;
                    break;
                // axiom
                case 'X':
                    break;
                default:
                    throw new InvalidOperationException("Invalid L-Tree Operation");
            }
        }
        PlantDNA plant = Tree.AddComponent<PlantDNA>();
        AssignFirstPlantRule(plant);
        return plant;
    }

This is the fitness function with raycast:

 public void FitnessFunction()
    {
        for (int i = 0; i < population.Count; i++)
        {
            if (Input.GetMouseButtonDown(0))
            {
                RaycastHit hit;
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(ray, out hit, 100.0f))
                {
                    if (hit.transform.gameObject == population[i].gameObject)
                    {
                        // Fitness Score um 1 erhöhen
                        population[i].fitnessScore += 1;
                        Debug.Log(population[i].fitnessScore);
                    }
                }
            }

        }
    }

plants



Solution 1:[1]

First, you need cahsed treeSegment.GetComponent<LineRenderer>() - its loaded method. Just For raycast you needed Collision component and RigitBody component on clickable object. I see, you additing mesh collider. But are you addition RigitBody?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Red-Cat-Fat