Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Instruction for SendMessageUpwards

Discussion in 'Scripting' started by alexander, Jul 25, 2008.

  1. alexander

    alexander

    Joined:
    Mar 28, 2008
    Posts:
    180
    Hi,

    tried out this command.
    Have two scripts attached to a GO called MainMenu.js and Thumb.js

    In the MainMenu.js I've got a button which should call a function in Thumb.js look like this:
    Code (csharp):
    1.  
    2. if (GUILayout.Button("",GUI.skin.customStyles[17])) {
    3.    SendMessageUpwards ("DoScrollControl", 500.0, 150.0, 400.0);
    4. }
    And in the Thumb.js I've got this function:
    Code (csharp):
    1. function DoScrollControl (xPos : float, yPos : float, width : float) {
    2.    print("DoScroll");
    3. }
    But I don't get a "DoScroll" in the command-line.
    But according to the scripting ref: http://unity3d.com/support/documentation/ScriptReference/Component.SendMessageUpwards.html
    it should be correct.

    Or not?
     
  2. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    If they are attached to the same gameobject you can just use SendMessage and all scripts should get the attached with the right function defined should get called.

    The Upwards version is only required if you have a scenario like this

    go - script 1
    - child - script 2

    calling SendMessageUpwards from script 2 will send the message to script 1. Atleast thats how i interpret the docs.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    With SendMessage you can use only one parameter.

    --Eric
     
  4. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    True, your script gives no errors?
     
  5. alexander

    alexander

    Joined:
    Mar 28, 2008
    Posts:
    180
    Hmm, than I get this message:
    Ah, don't saw your newer messages.
    With SendMessage(Upwards) I can send only one value?

    Is there another way for sending 3 values?
     
  6. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    To send multiple parameters with SendMessage you are gonna have to encapsulate them into one object. In your case using 3 parameters a Vector3 might be handy.

    Edit: Remember to change the DoScrollControl function parameters acordingly aswell.
     
  7. alexander

    alexander

    Joined:
    Mar 28, 2008
    Posts:
    180
    What way would this look like?

    And how can I readout this Vector3 in the Thumb.js-function?
     
  8. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    Code (csharp):
    1. if (GUILayout.Button("",GUI.skin.customStyles[17])) {
    2.    SendMessageUpwards("DoScrollControl", new Vector3(500.0, 150.0, 400.0));
    3. }
    4.  
    Code (csharp):
    1.  
    2. function DoScrollControl(input : Vector3)
    3. {
    4.  
    5.    // use input here
    6.    Debug.Log("(" + input.x + ", " + input.y + ", " + input.z + ")");
    7.  
    8. }
    9.  
    10.  
     
  9. alexander

    alexander

    Joined:
    Mar 28, 2008
    Posts:
    180
    That's it!
    Many thx to you.


    Problem solved in 25 minutes... :D
     
  10. 001visualizer

    001visualizer

    Joined:
    Aug 24, 2008
    Posts:
    113
    :arrow:

    Hi everybody. I am a new user here on that forum. And I am just testing Unity3D to comparing it with Quest3D.

    Communicating between different scripts is very essential in programming a games.

    You discussed above about the "SendMessageUpwards".

    Is there also a way in Unity3D to communicate between all GameObjects within the same level:
    >>something like "SendMessageSideway"??

    Or how do you "talk" between GameObjects which are on the same hierarchy level?
     
  11. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    Code (csharp):
    1.  
    2. // the message we want to send
    3. var message = "hi";
    4.  
    5. // parent is a transform. [url]http://unity3d.com/support/documentation/ScriptReference/Transform.html[/url]
    6. var parent = transform.parent;
    7.  
    8. // as mentioned in that doc page, transforms children are accessed by iterating through the transform
    9. for(var child : Transform in parent) {
    10.     // child is a transform. it has a SendMessage function that it inherits from Component
    11.     child.SendMessage("GreetFunction", message);
    12. }
    13.  
    14. // so what we have done is go to our parent and then get a list of children from it, which we iterate through. This will include sending a message to ourself.
    15.  
    16. // you can also use GetComponent and then call the function which is a lot faster than sending messages
    17. // it would be even faster to have the components cache themselves somewhere and then iterate through the cache of references
    18.  

    Pretty much if you want to do something in unity you can. :)