SendMessage with 2 arguments

I have this code:

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

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

why?

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

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

void BoxClicked( object[] args )
{

}

or look at using the UnityEvent system which supports up to 4 arguments.

Thank you for you answer Karl.Jones.

Next problem appeared.

This is at Script A:

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

    }

This is at Script B:

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

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

You need to unbox the objects.
You are also assigning the index, should be boxInfo[index]

E.G

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

ClickedWord and ClickedColor has to be integer

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

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!

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.

I’m still getting errors

    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 + ") ");

    }

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

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

Im posting the scripts again:

Script A:

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:

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”

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)

where did you get the 10 from?

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

// should be

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

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

These are the word and color arrays:

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"};

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.

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

2 Likes

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

http://docs.unity3d.com/Manual/MessagingSystem.html

Or just GetComponent().BoxClicked :slight_smile:

Never used the messagingsystem, it looks rather complicated. The documentation 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

I found the error.

The error was that the:
string[ ] Word and string[ ] 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

1 Like

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