Search Unity

Referencing a script on a GameObject in a MonoBehaviour in another GameObject

Discussion in 'Scripting' started by Minassian, Jul 21, 2011.

  1. Minassian

    Minassian

    Joined:
    Nov 22, 2010
    Posts:
    40
    I posted this on Unity Aswers some while ago and still haven't found a way to do it.
    Seems like a simple thing and very escential one if you try to write high cohesion code.

    So the documentation here says: "You can also expose references to other objects to the inspector. Below you can drag a game object that contains the OtherScript on the target slot in the inspector."

    Now what if I have several scripts of that type in that GameObject, only the first one seems to be referenced. Is there a way to pick one from the inspector ?


    Let me ask same question differently so there's no confusion.
    Lets say I publish a MonoBehaviour variable in a script:

    Code (csharp):
    1. public MonoBehaviour targetBehaviour;
    When added to a GameObject, this script allowes me to drag and drop any other script from that same GameObject into the 'targetBehaviour' variable.

    How can I reference a script component from ANOTHER GameObject that has MORE THAN ONE script. If I drag the GameObject, the first script seems to be referenced, I have no option to choose which one.
     
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    This is a bit fiddly but you can do it. You do it much like dragging the game object onto the expose variable but instead of dragging the whole game object onto it you can actually drag individual components by dragging the component title bar in the inspect.

    In your case your are going to need to open two inspector windows. First select the game object with the components you need to connect to your other script. Then lock one of the inspectors with the lock icon on the top right of the inspector window. Then select the game object with the script that needs the reference hooked up.

    You should now have two inspector windows open with each game object displayed in each inspector window. You can now drag individual components onto the reference.

    Hope that helps you out.
     
    michal-zetkowski and moonabook like this.
  3. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Also, if you know the particular script you want, you can make that your public variable instead of just having it be MonoBehaviour. So if your object has three scripts, "Explode", "RunAround", and "DoSomethingAwesome", you would put "public DoSomethingAwesome targetBehaviour" in your destination object. Then when you drag the first GameObject, it should automatically put the DoSomethingAwesome script there since it's the only script on the object that matches the type you specified.
     
  4. Sirex

    Sirex

    Joined:
    Feb 7, 2011
    Posts:
    77
    You need to explain why you have several scripts of one type on a gameobject.
    What i have done in similar cases it to convert the scripts in to gameobjects that hold the scripts.
    Example, i have a fleet gameobject with a fleet script. But then i have ships for the fleet, so i add
    a gameboejct array which holds ship gameobjects, which got one ship script each. Instead of just adding ship scripts.
    Then you have your calling script request the array with gameobjects.
     
  5. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    I think it's quite reasonable to have two scripts of the same type on 1 object. I have enemies with 1 weapon, 1 script. Enemies with two weapons, two scripts. These are very simple weapons that simply spawn objects are the enemy objects position so I don't think it warrants an entirely new game object.
     
  6. Sirex

    Sirex

    Joined:
    Feb 7, 2011
    Posts:
    77
    Okay i can agree to that. But i think some container is needed if you want to seperate scripts.
    Maybe find all scripts in the start() function and make a property to get them.
     
    Last edited: Jul 22, 2011
  7. Minassian

    Minassian

    Joined:
    Nov 22, 2010
    Posts:
    40
  8. johot

    johot

    Joined:
    Apr 11, 2011
    Posts:
    201
    This info was great!

    I had a problem with using EZ GUI where I can set a property for a button called "Script with method to invoke", since that property is of type MonoBehaviour the first script of a game object would be picked (in this case a Sprite script) when in fact I needed the seconds script. The solution proposed here worked very well. To bring up a second Inspector window I had to right click the Inspector tab (after clicking the lock), and then select Add Tab -> Inspector Window. I then had to redock the Inspector window. After those steps I could drag and drop the correct script (by dragging from it's header).

    I hope the Unity team can create a better solution for this in Unity 3.5, a drop down/popup when multiple choices exist perhaps?
     
  9. winxalex

    winxalex

    Joined:
    Jun 29, 2014
    Posts:
    166