- Home /
send message
I set UI button sendmessage to master script play the animator, but the console dispaly the message no receiver.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class mummy_attack : MonoBehaviour { GameObject mummy_01; Animator anim; // Start is called before the first frame update void Start() { anim = GetComponent();
}
// Update is called once per frame
public void OnButton()
{
SendMessage("MummyAttack");
}
}
using UnityEngine;
public class Player3DExample : MonoBehaviour {
public float moveSpeed = 8f;
public Joystick joystick;
Animator anim;
private Rigidbody rigidBody;
GameObject mummy_01;
void Start()
{
rigidBody = GetComponent<Rigidbody>();
anim = GetComponent<Animator>();
mummy_01 = GameObject.Find("mummy_01");
}
void Update()
{
Vector3 moveVector = (Vector3.right * joystick.Horizontal + Vector3.forward * joystick.Vertical);
if (moveVector != Vector3.zero)
{
transform.rotation = Quaternion.LookRotation(moveVector);
transform.Translate(moveVector * moveSpeed * Time.deltaTime, Space.World);
anim.SetBool("speed", true);
//anim.SetBool("Attack", false);
}
else
{
anim.SetBool("speed", false);
}
}
public void MummyAttack()
{
anim.SetBool("Attack", true);
}
}
Answer by Bunny83 · Feb 16, 2019 at 01:44 PM
To quote the documentation:
SendMessage calls the method named methodName on every MonoBehaviour in this game object.
So the component with the receiving method most likely is not attached to the same gameobject. You would need to send the message to the gameobject where the receiving script is attached to.
Though note that SendMessage is a rather outdated and slow technique. In most cases you want to setup an actual reference to the target script and just call the method directly. If you want more abstraction you can use a serialized UnityEvent which can be assigned in the inspector.
Your answer

Follow this Question
Related Questions
Disable and Enable UI text in function 1 Answer
How to use Unet to send network messages like RPC (Javascript) 0 Answers
Camera jitters / lag behind when jumping or falling 2 Answers
How do I create a First-Person View model with hands and multiple weapons? 0 Answers
Problems with velocity in Unity 5 3 Answers