# SendMessage with 2 arguments

**URL:** <https://discussions.unity.com/t/sendmessage-with-2-arguments/593114>\
**Category:** Scripting\
**Tags:** Scripting\
**Created:** [August 11, 2015, 11:28am UTC](https://discussions.unity.com/t/sendmessage-with-2-arguments/593114 "2015-08-11T11:28:25Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![MG](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@MG](https://discussions.unity.com/u/MG)\
**Post date:** [August 11, 2015, 11:28am UTC](https://discussions.unity.com/t/sendmessage-with-2-arguments/593114/1 "2015-08-11T11:28:25Z")

</div>

I have this code:

```csharp
using UnityEngine;
using System.Collections;

public class boxesInfo : MonoBehaviour {
    public string Word;
    public string Color;
    private GameObject TheSpawner;

    // Use this for initialization
    void Start () {
        TheSpawner = GameObject.FindGameObjectWithTag("TheSpawner");
    }
   
    // Update is called once per frame
    void Update () {
   
    }

    void Delete ()
    {
        Destroy (gameObject);
    }

    void OnMouseDown ()
    {
        TheSpawner.gameObject.SendMessage ("BoxClicked", Word, Color, SendMessageOptions.DontRequireReceiver);

    }
}

```

And i get this error

```csharp
Assets/Scripts/boxesInfo.cs(26,39): error CS1501: No overload for method `SendMessage' takes `4' arguments

```

why?

---

<div class="post-metadata">

**Author:** ![karl\_jones](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/karl_jones/32/83270_2.png) [@karl\_jones](https://discussions.unity.com/u/karl_jones)\
**Post date:** [August 11, 2015, 11:34am UTC](https://discussions.unity.com/t/sendmessage-with-2-arguments/593114/2 "2015-08-11T11:34:46Z")

</div>

SendMessage only supports a single value, if you want to send multiple then wrap them up in a struct or array of objects.

```auto
 SendMessage ("BoxClicked", new object[]{ Word, Color }, SendMessageOptions.DontRequireReceiver)

void BoxClicked( object[] args )
{

}

```

or look at using the [UnityEvent](http://docs.unity3d.com/ScriptReference/Events.UnityEvent.html)system which supports up to 4 arguments.

---

<div class="post-metadata">

**Author:** ![MG](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@MG](https://discussions.unity.com/u/MG)\
**Post date:** [August 11, 2015, 11:50am UTC](https://discussions.unity.com/t/sendmessage-with-2-arguments/593114/3 "2015-08-11T11:50:28Z")

</div>

Thank you for you answer Karl.Jones.

Next problem appeared.

This is at Script A:

```csharp
void OnMouseDown ()
    {
        TheSpawner.gameObject.SendMessage ("BoxClicked", new object[]{Word, Color}, SendMessageOptions.DontRequireReceiver);

    }

```

This is at Script B:

```csharp
void BoxClicked(object[] boxInfo) // This is linje 325
    {

        for (int i = 0; i<10; i++) {

            if(boxInfo[0] == Word[i]){
                ClickedWord = i;
            }
            if(boxInfo[1] == Color[i]){
                ClickedColor = i;
            }

        }
        Debug.Log ("Clicked Word: " + boxInfo [0] + "( " + ClickedWord + ") Clicked Color: " + boxInfo [1] + "( " + ClickedColor + ") ");

    }// This is line 341

```

And im getting this error

```csharp
IndexOutOfRangeException: Array index is out of range.
SpawnControls.BoxClicked (System.Object[] boxInfo) (at Assets/SpawnControls.cs:331)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo

```

---

<div class="post-metadata">

**Author:** ![karl\_jones](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/karl_jones/32/83270_2.png) [@karl\_jones](https://discussions.unity.com/u/karl_jones)\
**Post date:** [August 11, 2015, 11:52am UTC](https://discussions.unity.com/t/sendmessage-with-2-arguments/593114/4 "2015-08-11T11:52:18Z")

</div>

You need to [unbox](https://msdn.microsoft.com/en-us/library/yz2be5wk.aspx)the objects.  
You are also assigning the index, should be boxInfo[index]

E.G

```auto
ClickedColor = (Color)boxInfo[1];

```

```auto
void BoxClicked(object[] boxInfo)
{
    ClickedWord = (string)boxInfo[0];
    ClickedColor = (Color)boxInfo[1];
}

```

---

<div class="post-metadata">

**Author:** ![MG](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@MG](https://discussions.unity.com/u/MG)\
**Post date:** [August 11, 2015, 12:01pm UTC](https://discussions.unity.com/t/sendmessage-with-2-arguments/593114/5 "2015-08-11T12:01:55Z")

</div>

ClickedWord and ClickedColor has to be integer

This is how the Word and color look like, when i want to compare it:

```csharp
public string[] Word = new string[] {"Black","Blue","Brown","Green","Orange","Pink","Purple","Red","White","Yellow"};
    public string[] Color = new string[] {"Black","Blue","Brown","Green","Orange","Pink","Purple","Red","White","Yellow"};

```

FYI, on SendMessage, the Word and Color is Strings!

---

<div class="post-metadata">

**Author:** ![karl\_jones](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/karl_jones/32/83270_2.png) [@karl\_jones](https://discussions.unity.com/u/karl_jones)\
**Post date:** [August 11, 2015, 12:18pm UTC](https://discussions.unity.com/t/sendmessage-with-2-arguments/593114/6 "2015-08-11T12:18:10Z")

</div>

Well if they are both strings then pass an array of strings instead of objects, makes life a little simpler and you wont need to unbox.

---

<div class="post-metadata">

**Author:** ![MG](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@MG](https://discussions.unity.com/u/MG)\
**Post date:** [August 11, 2015, 12:24pm UTC](https://discussions.unity.com/t/sendmessage-with-2-arguments/593114/7 "2015-08-11T12:24:38Z")

</div>

I’m still getting errors

```csharp
    void BoxClicked(string[] boxInfo)
    {

        for (int i = 0; i<10; i++) {

            if(string.Compare(boxInfo[0], Word[i]) == 1) {
                ClickedWord = i;
            }
            if(string.Compare(boxInfo[1], Color[i]) == 1){
                ClickedColor = i;
            }

        }
        Debug.Log ("Clicked Word: " + boxInfo [0] + "( " + ClickedWord + ") Clicked Color: " + boxInfo [1] + "( " + ClickedColor + ") ");

    }

```

---

<div class="post-metadata">

**Author:** ![karl\_jones](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/karl_jones/32/83270_2.png) [@karl\_jones](https://discussions.unity.com/u/karl_jones)\
**Post date:** [August 11, 2015, 12:35pm UTC](https://discussions.unity.com/t/sendmessage-with-2-arguments/593114/8 "2015-08-11T12:35:11Z")

</div>

if boxInfo is an array of objects then you need to unbox them into strings or pass an array of strings like I suggested.

```auto
if((string)boxInfo[0]== Word[i]){

```

---

<div class="post-metadata">

**Author:** ![MG](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@MG](https://discussions.unity.com/u/MG)\
**Post date:** [August 11, 2015, 12:42pm UTC](https://discussions.unity.com/t/sendmessage-with-2-arguments/593114/9 "2015-08-11T12:42:54Z")

</div>

Im posting the scripts again:

Script A:

```csharp
using UnityEngine;
using System.Collections;

public class boxesInfo : MonoBehaviour {
    public string Word;
    public string Color;
    private GameObject TheSpawner;

    // Use this for initialization
    void Start () {
        TheSpawner = GameObject.FindGameObjectWithTag("TheSpawner");
    }
   
    // Update is called once per frame
    void Update () {
   
    }

    void Delete ()
    {
        Destroy (gameObject);
    }

    void OnMouseDown ()
    {
        TheSpawner.gameObject.SendMessage ("BoxClicked", new string[]{Word, Color}, SendMessageOptions.DontRequireReceiver);

    }
}

```

Script B:

```csharp
void BoxClicked(string[] boxInfo) // Line 325
    {

        for (int i = 0; i<10; i++) {

            if(string.Compare(boxInfo[0], Word[i]) == 1) {
                ClickedWord = i;
            }
            if(string.Compare(boxInfo[1], Color[i]) == 1){
                ClickedColor = i;
            }

        }
        Debug.Log ("Clicked Word: " + boxInfo [0] + "( " + ClickedWord + ") Clicked Color: " + boxInfo [1] + "( " + ClickedColor + ") ");

    } // Line 341

```

The stupid error, when i Activate the “OnMouseDown”

```csharp
IndexOutOfRangeException: Array index is out of range.
SpawnControls.BoxClicked (System.String[] boxInfo) (at Assets/SpawnControls.cs:331)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232)
System.MonoType.InvokeMember (System.String name, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object target, System.Object[] args, System.Reflection.ParameterModifier[] modifiers, System.Globalization.CultureInfo culture, System.String[] namedParameters) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/MonoType.cs:433)
UnityEngine.SetupCoroutine.InvokeMember (System.Object behaviour, System.String name, System.Object variable) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:18)
UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
boxesInfo:OnMouseDown() (at Assets/Scripts/boxesInfo.cs:26)
UnityEngine.SendMouseEvents:smile:oSendMouseEvents(Int32, Int32)

```

---

<div class="post-metadata">

**Author:** ![LeftyRighty](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@LeftyRighty](https://discussions.unity.com/u/LeftyRighty)\
**Post date:** [August 11, 2015, 12:51pm UTC](https://discussions.unity.com/t/sendmessage-with-2-arguments/593114/10 "2015-08-11T12:51:31Z")

</div>

where did you get the 10 from?

```auto
for(int i =0; i<10; i++)

// should be

for(int i =0; i<boxInfo.length; i++)

```

---

<div class="post-metadata">

**Author:** ![MG](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@MG](https://discussions.unity.com/u/MG)\
**Post date:** [August 11, 2015, 1:01pm UTC](https://discussions.unity.com/t/sendmessage-with-2-arguments/593114/11 "2015-08-11T13:01:05Z")

</div>

LeftRighty no. it should be the lenght of word[&nbsp;] or color[&nbsp;]

These are the word and color arrays:

```csharp
public string[] Word = new string[] {"Black","Blue","Brown","Green","Orange","Pink","Purple","Red","White","Yellow"};
    public string[] Color = new string[] {"Black","Blue","Brown","Green","Orange","Pink","Purple","Red","White","Yellow"};

```

---

<div class="post-metadata">

**Author:** ![karl\_jones](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/karl_jones/32/83270_2.png) [@karl\_jones](https://discussions.unity.com/u/karl_jones)\
**Post date:** [August 11, 2015, 1:06pm UTC](https://discussions.unity.com/t/sendmessage-with-2-arguments/593114/12 "2015-08-11T13:06:31Z")

</div>

You dont need to use string.Compare, == is fine.  
The issue seems to be your boxInfo array.  
Print out the size of it in a debug statement, it should be 2.

---

<div class="post-metadata">

**Author:** ![TJHeuvel-net](https://avatars.discourse-cdn.com/v4/letter/t/8797f3/32.png) [@TJHeuvel-net](https://discussions.unity.com/u/TJHeuvel-net)\
**Post date:** [August 11, 2015, 1:09pm UTC](https://discussions.unity.com/t/sendmessage-with-2-arguments/593114/13 "2015-08-11T13:09:12Z")

</div>

Perhaps SendMessage isnt the best option here? You can just link in the second script and call the function directly.

---

<div class="post-metadata">

**Author:** ![karl\_jones](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/karl_jones/32/83270_2.png) [@karl\_jones](https://discussions.unity.com/u/karl_jones)\
**Post date:** [August 11, 2015, 1:17pm UTC](https://discussions.unity.com/t/sendmessage-with-2-arguments/593114/14 "2015-08-11T13:17:22Z")

</div>

Like I said earlier, the UnityEvent / MessagingSystem is perfect for this 😉

> **[Unity - Manual: UnityEvents](https://docs.unity3d.com/Manual/UnityEvents.html)**

[http://docs.unity3d.com/Manual/MessagingSystem.html](http://docs.unity3d.com/Manual/MessagingSystem.html)

---

<div class="post-metadata">

**Author:** ![TJHeuvel-net](https://avatars.discourse-cdn.com/v4/letter/t/8797f3/32.png) [@TJHeuvel-net](https://discussions.unity.com/u/TJHeuvel-net)\
**Post date:** [August 11, 2015, 1:20pm UTC](https://discussions.unity.com/t/sendmessage-with-2-arguments/593114/15 "2015-08-11T13:20:26Z")

</div>

Or just GetComponent().BoxClicked 🙂

Never used the messagingsystem, it looks rather complicated. The [documentation](http://docs.unity3d.com/ScriptReference/EventSystems.ExecuteEvents.Execute.html)is also lacking i think, there really isnt a demonstration on how to use this.

---

<div class="post-metadata">

**Author:** ![MG](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@MG](https://discussions.unity.com/u/MG)\
**Post date:** [August 11, 2015, 2:30pm UTC](https://discussions.unity.com/t/sendmessage-with-2-arguments/593114/16 "2015-08-11T14:30:29Z")

</div>

> [@TJHeuvel.net](#):
>
> Or just GetComponent().BoxClicked 🙂
> 
> Never used the messagingsystem, it looks rather complicated. The [documentation](http://docs.unity3d.com/ScriptReference/EventSystems.ExecuteEvents.Execute.html)is also lacking i think, there really isnt a demonstration on how to use this.

There is 100 Object that have this script, just so you know it, and i want to get information from each of them when i want

---

<div class="post-metadata">

**Author:** ![MG](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@MG](https://discussions.unity.com/u/MG)\
**Post date:** [August 12, 2015, 2:37am UTC](https://discussions.unity.com/t/sendmessage-with-2-arguments/593114/17 "2015-08-12T02:37:36Z")

</div>

I found the error.

The error was that the:  
string[&nbsp;] Word and string[&nbsp;] Color  
Wasn’t update to its newest editing, so apperently it had only a length of 6, which was the old length.  
I deleted all the strings values in the Inspector and changed it by hand

It is now working like a charm

---

<div class="post-metadata">

**Author:** ![karl\_jones](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/karl_jones/32/83270_2.png) [@karl\_jones](https://discussions.unity.com/u/karl_jones)\
**Post date:** [August 12, 2015, 9:47am UTC](https://discussions.unity.com/t/sendmessage-with-2-arguments/593114/18 "2015-08-12T09:47:23Z")

</div>

Yes if you make them public they can be changed, use .Length instead of a literal value.
