Instruction for SendMessageUpwards

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:

if (GUILayout.Button("",GUI.skin.customStyles[17])) {
   SendMessageUpwards ("DoScrollControl", 500.0, 150.0, 400.0);
}

And in the Thumb.js I’ve got this function:

function DoScrollControl (xPos : float, yPos : float, width : float) {
   print("DoScroll");
}

But I don’t get a “DoScroll” in the command-line.
But according to the scripting ref: Unity - Scripting API: Component.SendMessageUpwards
it should be correct.

Or not?

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.

With SendMessage you can use only one parameter.

–Eric

True, your script gives no errors?

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?

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.

What way would this look like?

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

if (GUILayout.Button("",GUI.skin.customStyles[17])) {
   SendMessageUpwards("DoScrollControl", new Vector3(500.0, 150.0, 400.0));
}
function DoScrollControl(input : Vector3)
{

   // use input here
   Debug.Log("(" + input.x + ", " + input.y + ", " + input.z + ")");

}

That’s it!
Many thx to you.

Problem solved in 25 minutes… :smile:

: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?

// the message we want to send
var message = "hi";

// parent is a transform. [url]http://unity3d.com/support/documentation/ScriptReference/Transform.html[/url]
var parent = transform.parent;

// as mentioned in that doc page, transforms children are accessed by iterating through the transform
for(var child : Transform in parent) {
    // child is a transform. it has a SendMessage function that it inherits from Component
    child.SendMessage("GreetFunction", message);
}

// 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. 

// you can also use GetComponent and then call the function which is a lot faster than sending messages
// it would be even faster to have the components cache themselves somewhere and then iterate through the cache of references

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