Any idea how to merge 2 scripts or code OnMouseDrag() from another script

Hey,

tl;dr: I want to know if it’s either possible to merge 2 scripts or if it’s possible to code OnMouseDrag() for one gameobject from a script from a different gameobject or if there is a different solution

in my 2D project I got 2 main scripts right now and to make things easier, or more unified, I wondered if I could merge these scripts into one.

The project consists of several (complex) gameobjects which can be dragged around the screen and connected via lines.

The line is drawn after clicking one element of the complex gameobjects, which is realized with script #1 for this one element, after clicking it follows the mouse cursor. Now the logic behind connecting the line to another game object is realized in the LineRenderer gameobject and its script (script #2).

The main reason I have 2 script is due to the OnMouseDrag function, which is used in script #1 and I don’t know, if it’s possible to code this function from another script (such as script #2).

But for example right now, I need to reference the the object, which I click in order to draw the line, but this object is in script #1 and I don’t know how to reference it from script #2.

Script #1 looks like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Draw_Connecting_Line : MonoBehaviour {

	// (As of right now) This class 

	private LineRenderer lineRenderer = new LineRenderer ();
		
	public Transform origin;
	public Transform destin;

	void Awake () {

		origin = GetComponent<Transform> ();

		lineRenderer = GameObject.Find ("Line").GetComponent<LineRenderer> ();
	}

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	void OnMouseDrag () {

		Vector2 screenPos = new Vector2();
		Camera.main.ScreenToWorldPoint (screenPos);

		lineRenderer.SetPosition (0,
			new Vector3 (origin.position.x + (GetComponent<SpriteRenderer>().bounds.size.x)/2,
				origin.position.y,
				origin.position.z));
		lineRenderer.SetPosition (1, Camera.main.ScreenToWorldPoint(Input.mousePosition));
	}
}

Script #2 looks like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LineScript : MonoBehaviour {

	// (As of right now) This script draws the line following the mouse and checks if the mouse
	// collides with the bounding box of the input of another block

	private LineRenderer line = new LineRenderer();

	private GameObject goalInput;
	private GameObject[] goalInputs;
	private GameObject[] goalInputs2;
	private CircleCollider2D circCol;
	private CircleCollider2D[] circCols;

	private Transform origin, destin;

	private BlockMovingScript originBlockScript, destinBlockScript;

	void Awake () {
		goalInputs = GameObject.FindGameObjectsWithTag ("input");
		circCols = new CircleCollider2D[goalInputs.Length];

		for(int i = 0; i < goalInputs.Length; i++) {
			circCols _= goalInputs*.GetComponent<CircleCollider2D>();*_

* }*

* line = GetComponent ();*
* }*

* // Use this for initialization*
* void Start () {*

* }*

* // Update is called once per frame*
* void Update () {*

* ScanInput ();*

* Vector3 mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);*
* mousePos.z = 0;*

* // Run through all circle colliders and check*

* for (int i = 0; i < circCols.Length; i++) {*
_ if (circCols*.bounds.Contains (mousePos)) {
destin = circCols .transform;
line.SetPosition (1, new Vector3 (
destin.position.x - (destin.GetComponent ().bounds.size.x) / 2,
destin.position.y,
destin.position.z));*_

_ if (circCols .CompareTag (“inputA”)) {
destinBlockScript = circCols .GetComponentInParent ();
* destinBlockScript.setInputA ();
} else if (circCols .CompareTag (“inputB”)) {
destinBlockScript = circCols .GetComponentInParent ();
destinBlockScript.setInputB ();
}
}
}
}*_

* void ScanInput () {*
* goalInputs = FindGameObjectsWithDifferentTags(new string[] {“inputA”, “inputB”});*

* circCols = new CircleCollider2D[goalInputs.Length];*

* for (int i = 0; i < goalInputs.Length; i++) {*
circCols = goalInputs .GetComponent ();
* }*
* }*

* public static GameObject[] FindGameObjectsWithDifferentTags(string[] tags) {*
* List list = new List ();*
* foreach (string tag in tags) {*
* GameObject[] objs = GameObject.FindGameObjectsWithTag (tag);*
* list.AddRange (objs);*
* }*
* return list.ToArray ();*
* }*
}

public class Draw_Connecting_Line : MonoBehaviour {

	public static Draw_Connecting_Line current; // Add this

	void OnMouseDown () { //
		current = this;   // Add this
	}                     //
}

Then you can get current in any script by Draw_Connecting_Line.current.
Have a look at Statics.