0

Hy guys,

I have two scripts. One UI script which attached to the button (OnClick Event) in Unity editor. And Script that drawing line with line render.

I used to keep DrawBoundary() in Update now I so every time I click with the mouse button it will start drawing. I would like to add a UI button. So mouse click works only when I click UI button first. And only then I can draw a line with a mouse click.

I have created an event in UI and subscribe to this event in main script, but it doesn't work. Basically the DrawBoundary() function has to run continuously after bUI button was clicked.

I assume I have to pass bool variable to main script somehow ? So I can have to functions in UI Script. When button clicked I draw, when button click again I stop.

I am trying to this too scripts completely unaware of each other...

Thank you

Main Script:

void Start()
    {
        UISystem.onClick += DrawBoundary;

    }


    // Update is called once per frame
    void Update()
    {
        //DrawBoundary(); Before I had this method in Update
    }


    //Add point on mouse click
    public void DrawBoundary()
    {
        if (Input.GetMouseButtonDown(0))
        {
            //Adds position to array
            _lineRenderer.positionCount++; 

            // Sets new position
            _lineRenderer.SetPosition(_positionIndexCount, GetMouseWorldPosition());

            //Changes to next position index
            _positionIndexCount++;

            //Instantiate point icons
            Instantiate(boundaryPoint, GetMouseWorldPosition(), Quaternion.identity);

            BoundaryCollider2D();
        }

        //Avoid loop line when second pointed is added
        if (_positionIndexCount > 2)
        {
            _lineRenderer.loop = true;
        }    
    }

UI Script with Event:

 public delegate void ActionClick();
 public static event ActionClick onClick;

    public void ButtonClick()
    {
        if (onClick != null) 
        {
            onClick(); 
        }
    }

1 Answer 1

2

Yes you need a bool for that. You will check the bool in your update function and draw when its true. You can add one in your ui script with "public static" and you can also access it from your main script. Then when you press the button you will change the value of your bool.

Like:

public static bool isdraw=false;    
public void ButtonClick()
{
    if (isdraw) 
    {
        isdraw=false;
    }
    else
        isdraw=true;
}

and in main:

void Update()
{
    if(UI.isdraw)
        DrawBoundary();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.