Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Changing pointer of one object to another

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

  1. som3guy

    som3guy

    Joined:
    Sep 14, 2017
    Posts:
    13
    Hey,

    I think this is a really simple issue but I still struggle finding out the best solution for this.

    In my scenario I connect a line with a gameobject, let go of the mouse button, and then this gameobject saves the connected line as a property called connectedLine. At the same time, the line object also has a property "targetObject" which is set to the collisionObject so that it knows where it's connected to.

    Now I want to take a line and connect it to a different gameobject, which means that the targetObject and connectedLine property need to be updated. More specifically, in the old connected gameobject the connectedLine property needs to be set to null and the new gameobject will get this line, and the targetObject of the line needs to be updated to the new gameobject as well.

    The problem is that I need to resolve this circular statement in line 15-22. I set the connectedLine of the old object to null and therefore cannot access it anymore and set the other properties.

    And btw line 6-10 is analog but it looks different as I have been focused on fixing the other if case as it's easier to debug.

    So, do I need to make a temporary object and if so, how? Or is the problem due to call by value/call by reference?

    Code (CSharp):
    1.  
    2. void OnMouseUp() {
    3.  
    4.         if (collisionObject && collisionObject.CompareTag ("inputPin")) {
    5.             if (collisionObject.GetComponent<Scene2_RightCircle> ().connectedLine) {
    6.                 // Line is already connected
    7.                 connectedLine.GetComponent<Scene2_Line>().targetObject.GetComponent<Scene2_RightCircle>().connectedLine = null;
    8.                 Destroy(collisionObject.GetComponent<Scene2_RightCircle>().connectedLine.gameObject);
    9.                 collisionObject.GetComponent<Scene2_RightCircle>().connectedLine = connectedLine;
    10.                 connectedLine.GetComponent<Scene2_Line> ().targetObject = collisionObject.gameObject;
    11.             } else {
    12.                 // No line connected
    13.  
    14.                 // Delete line pointer at current pin
    15.                 connectedLine.GetComponent<Scene2_Line>().targetObject.GetComponent<Scene2_RightCircle>().connectedLine = null;
    16.  
    17.                 // Set connectedLine in new target to this line
    18.                 collisionObject.GetComponent<Scene2_RightCircle> ().connectedLine = connectedLine;  
    19.                 Debug.Log(collisionObject.GetComponent<Scene2_RightCircle>().connectedLine);
    20.  
    21.                 // Set target of line to this new pin
    22.                 connectedLine.GetComponent<Scene2_Line> ().targetObject = collisionObject.gameObject;  
    23.             }
    24.         } else if (!collisionObject) {
    25.             Destroy (connectedLine);
    26.         }
    27.     }
    28.  
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    So, it sounds like (psuedo code):
    Code (csharp):
    1. obj.connectedLine = someLine;
    2. someLine.targetObject = obj;
    3.  
    4. // update the line:
    5. obj.connectedLine = null;
    6.  
    7. //   obj2 (new target)
    8. obj2.connectedLine = someLine;
    9. someLine.targetObject = obj2;
    And sure, if you don't have access to those variables directly, then use a temporary variable to store the line.