- Home /
Question by
mzeman2203 · Apr 28, 2021 at 03:53 PM ·
c#sendmessage
Sendmessage converting gameObject to object
I want to be able to send which gameobject the user has clicked on with a sendmessage. In my cursor script I have this code
if (hit.collider.transform.parent != null && hit.collider.transform.parent.name == "townhall") )
{
gameObject.SendMessage("movingObject", hit.collider.transform.parent.gameObject,
SendMessageOptions.DontRequireReceiver);
}
hit is just a RaycastHit
. And in my builder script I have this
public void movingObject(GameObject objects)
{
print(objects);
}
But I keep getting the error
ArgumentException: Object of type 'UnityEngine.Object' cannot be converted to type 'UnityEngine.GameObject'.
Whats the reason for this? It says it can't convert an object to a gameobject, but to my knowledge I am not working with any objects only gameobjects
Comment
Why are you using SendMessage
in the first place? Can't you retrieve the component attached to the object and call the method properly?
GetComponent<SCRIPTNAME>().movingObject(hit.collider.transform.parent.gameObject);