Search Unity

  1. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  2. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  3. If you couldn't join the live stream, take a peek at what you missed.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Issue with referencing certain properties during events

Discussion in '2D' started by som3guy, May 17, 2018.

  1. som3guy

    som3guy

    Joined:
    Sep 14, 2017
    Posts:
    13
    Hey,

    so this is rather hard to describe but please bear with me.

    I am drawing a line from elements of type A to elements of type B in my project and after drawing, the B object saves the line as a property and the line knows where it's connected to. This is done mainly via OnMouseDown and OnMouseDrag on an A object. This works very well so far.

    However, when releasing the mouse button and finishing drawing the line, OnMouseUp (which is in the A object) triggers obviously. BUT when this happens, I want to check a certain property of the B object.

    More precisely, during OnMouseUp I want to check if there is a line already connected to the B object and if so, replace the line with the currently drawn line. Now the problem is that I have no idea how to check this in the script in the A object, especially in the OnMouseUp event.

    Here's the code:

    Class A:

    Code (CSharp):
    1.     GameObject line;
    2.     Vector2[] tempEdges;
    3.  
    4.     void OnMouseDown () {
    5.  
    6.         // instantiate Line after clicking circle
    7.         line = Instantiate (Resources.Load("Line")) as GameObject;
    8.         Manager.currentlyDrawnLine = line.gameObject;
    9.     }
    10.  
    11.     void OnMouseDrag () {
    12.  
    13.         if (!Manager.isMouseInsideCircle) {
    14.             Vector2 screenPos = new Vector2 ();
    15.             Camera.main.ScreenToWorldPoint (screenPos);
    16.  
    17.             line.GetComponent<LineRenderer> ().SetPosition (0,
    18.                 new Vector3 (transform.position.x + (GetComponent<SpriteRenderer> ().bounds.size.x) / 2,
    19.                     transform.position.y,
    20.                     transform.position.z));
    21.             line.GetComponent<LineRenderer> ().SetPosition (1, Camera.main.ScreenToWorldPoint (Input.mousePosition) + Vector3.forward * 10);
    22.  
    23.             tempEdges = line.GetComponent<EdgeCollider2D> ().points;
    24.             tempEdges [0] = new Vector2 (
    25.                 transform.position.x + (GetComponent<SpriteRenderer> ().bounds.size.x) / 2 - 0.7f,
    26.                 transform.position.y - 0.217f);
    27.             tempEdges [1] = new Vector2 (
    28.                 (Camera.main.ScreenToWorldPoint (Input.mousePosition) + Vector3.forward * 10).x - 0.7f,
    29.                 (Camera.main.ScreenToWorldPoint (Input.mousePosition) + Vector3.forward * 10).y - 0.217f);
    30.  
    31.             line.GetComponent<EdgeCollider2D> ().points = tempEdges;
    32.         }
    33.     }
    34.  
    35.     void OnMouseUp() {
    36.  
    37.     // Here is where I want to check if there is already a line connected to the B object
    38.  
    39.         }
    40.         if (!Manager.currentlyDrawnLine.GetComponent<Line>().destinObject) {
    41.             Destroy (Manager.currentlyDrawnLine);
    42.         }
    43.  
    44.         Manager.currentlyDrawnLine = null;
    45.     }
    Class B:

    Code (CSharp):
    1.     public GameObject connectedLine;
    2.     public bool isConnected = false;
    3.  
    4.     void Update() {
    5.  
    6. //        Debug.Log (Manager.currentlyDrawnLine);
    7.  
    8.         if (connectedLine) {
    9.             GetComponent<SpriteRenderer> ().color = Color.green;
    10.         } else {
    11.             GetComponent<SpriteRenderer> ().color = Color.white;
    12.         }
    13.  
    14.         mousePos.z = 0;
    15.  
    16.         if (GetComponent<CircleCollider2D> ().bounds.Contains (mousePos)) {
    17.             Manager.isMouseInsideCircle = true;
    18.         } else if (!GetComponent<CircleCollider2D> ().bounds.Contains (mousePos)) {
    19.             Manager.isMouseInsideCircle = false;
    20.         }
    21.          
    22.         // If mouse inside circle and a line being drawn
    23.         if (Manager.isMouseInsideCircle && Manager.currentlyDrawnLine) {
    24.             Manager.currentlyDrawnLine.GetComponent<LineRenderer> ().SetPosition (1, this.transform.position);
    25.  
    26.             connectedLine = Manager.currentlyDrawnLine;
    27.             connectedLine.GetComponent<Line> ().destinObject = this.gameObject;
    28.  
    29.             // If mouse not inside circle and a line being drawn
    30.         } else if (!Manager.isMouseInsideCircle && Manager.currentlyDrawnLine) {
    31.             if (connectedLine && !connectedLine.GetComponent<Line> ().isSnapped) {
    32.                 if (connectedLine) {
    33.                     connectedLine.GetComponent<Line> ().destinObject = null;
    34.                 }
    35.                 connectedLine = null;
    36.             }
    37.  
    38.         }
    39.     }
    Manager class:

    Code (CSharp):
    1. public static class Manager {
    2.  
    3.     public static GameObject currentlyDrawnLine;
    4.     public static bool isMouseInsideCircle = false;
    5.     public static GameObject alreadyConnectedLine;
    6.  
    7. }
    And you can find the repo here in case it helps: https://github.com/s0m3guy/MA_Sandbox
     
    Last edited: May 17, 2018
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    701
    If I'm understanding correctly, we can click and drag the A objects in your project which connect to B objects with lines. Is this correct?

    How do we know which B object we want to check? We should be able to generalize the answer to this into the answer to your question of how to check the B object.

    Does it use the nearest B object? Is there only one B object?
     
  3. som3guy

    som3guy

    Joined:
    Sep 14, 2017
    Posts:
    13
    You cannot drag the A objects, clicking them instantiates a line, which starts at this object and the end point follows the mouse while OnMouseDrag in order to then connect the line to an object of type B.

    >How do we know which B object we want to check?

    When I connect A with B I want to check if B already has a line connected to it. And B is also the object where I let go of the mouse button.

    And in this part here I check if I collide with a certain B object:

    In B class:
    Code (CSharp):
    1. if (GetComponent<CircleCollider2D> ().bounds.Contains (mousePos)) {
    2.             Manager.isMouseInsideCircle = true;
    And no there are several B objects
     
  4. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    701
    You could check for objects under the cursor when mouse is released using Camera.ScreenToWorldPoint and Physics2D.OverlapObject. If a B object is identified, you can access a script in that object to see what the values of its members are.
     
  5. som3guy

    som3guy

    Joined:
    Sep 14, 2017
    Posts:
    13
    Ooh that sounds good! Not sure yet how to work these things in but I am trying :)