Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by som3guy · Oct 25, 2017 at 11:16 AM · scripting problemonmousedrag

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 ();
     }
 }



Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

172 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges