Daniel Lip
Daniel Lip

Reputation: 11317

How can I disable/enable LineRenderer on mouse click on ogameobject?

The problem is that that line is always false:

private void OnMouseOver()
{
    if(Input.GetMouseButtonDown(0))
    {
        line.enabled = !line;
        if (line.enabled == true)
            CreatePoints();
    }
}

I want that when I click on the GameObject once the line will be enabled true and then CreatePoints() and once clicking again line will be false and won't create the points.

UPDATE:

Added a counter so the default state when running the game will be true and then it will switch to false/true.

int clickcount = 0;
    private void OnMouseOver()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (clickcount == 0)
            {
                line.enabled = true;
            }
            else
            {
                line.enabled = !line.enabled;
            }
            if (line.enabled)
                CreatePoints();

            clickcount ++;
        }
    }

This is working fine but is it a good way to use a counter like that ?

Upvotes: 1

Views: 5964

Answers (1)

killer_mech
killer_mech

Reputation: 1588

 if (Input.GetMouseButtonDown(0))
 {
   line.enabled = !line.enabled;
   if (line.enabled)
     CreatePoints();
 }

You cannot access enabled/disabled state of the gameobject directly. You need to retrieve it using .enabled and set the opposite of it to toggle on & off.

Upvotes: 2

Related Questions