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[i] = goalInputs[i].GetComponent<CircleCollider2D>();
}
line = GetComponent<LineRenderer> ();
}
// 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[i].bounds.Contains (mousePos)) {
destin = circCols [i].transform;
line.SetPosition (1, new Vector3 (
destin.position.x - (destin.GetComponent<SpriteRenderer> ().bounds.size.x) / 2,
destin.position.y,
destin.position.z));
if (circCols [i].CompareTag ("inputA")) {
destinBlockScript = circCols [i].GetComponentInParent<BlockMovingScript> ();
destinBlockScript.setInputA ();
} else if (circCols [i].CompareTag ("inputB")) {
destinBlockScript = circCols [i].GetComponentInParent<BlockMovingScript> ();
destinBlockScript.setInputB ();
}
}
}
}
void ScanInput () {
goalInputs = FindGameObjectsWithDifferentTags(new string[] {"inputA", "inputB"});
circCols = new CircleCollider2D[goalInputs.Length];
for (int i = 0; i < goalInputs.Length; i++) {
circCols [i] = goalInputs [i].GetComponent<CircleCollider2D> ();
}
}
public static GameObject[] FindGameObjectsWithDifferentTags(string[] tags) {
List<GameObject> list = new List<GameObject> ();
foreach (string tag in tags) {
GameObject[] objs = GameObject.FindGameObjectsWithTag (tag);
list.AddRange (objs);
}
return list.ToArray ();
}
}
Answer by Hatsuko · Oct 26, 2017 at 05:01 PM
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.
Your answer
Follow this Question
Related Questions
[Roguelike 2D] Why can't I implement my Wall script? 0 Answers
Unity animator always running only one animation...... 1 Answer
The associated script cannot be loaded- How to fix? 1 Answer
Unity compiler is asking me to add a semi colon even though I dont need it. 0 Answers
How do I change the scene after 2 objects are destroyed (SetActive false) 1 Answer