Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  5. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    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.         Vector3 mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
    15.         mousePos.z = 0;
    16.  
    17.         if (GetComponent<CircleCollider2D> ().bounds.Contains (mousePos)) {
    18.             Manager.isMouseInsideCircle = true;
    19.         } else if (!GetComponent<CircleCollider2D> ().bounds.Contains (mousePos)) {
    20.             Manager.isMouseInsideCircle = false;
    21.         }
    22.          
    23.         // If mouse inside circle and a line being drawn
    24.         if (Manager.isMouseInsideCircle && Manager.currentlyDrawnLine) {
    25.             Manager.currentlyDrawnLine.GetComponent<LineRenderer> ().SetPosition (1, this.transform.position);
    26.  
    27.             connectedLine = Manager.currentlyDrawnLine;
    28.             connectedLine.GetComponent<Line> ().destinObject = this.gameObject;
    29.  
    30.             // If mouse not inside circle and a line being drawn
    31.         } else if (!Manager.isMouseInsideCircle && Manager.currentlyDrawnLine) {
    32.             if (connectedLine && !connectedLine.GetComponent<Line> ().isSnapped) {
    33.                 if (connectedLine) {
    34.                     connectedLine.GetComponent<Line> ().destinObject = null;
    35.                 }
    36.                 connectedLine = null;
    37.             }
    38.  
    39.         }
    40.     }
    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:
    703
    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:
    703
    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 :)