SendMessage

Hello guys. I am trying to send a 1.0f message from one game object to another. Here are the bones of what i am trying to do:

        // 1.0f Message from -------------------------
    public class TouchButtonTalk : TouchLogic 
    {
    public Controller2D controller2D;      
        void OnTouchBegan()
        {
    		controller2D.SendMessage("horizontalMove", 1.0f, SendMessageOptions.DontRequireReceiver);
        }
    }
    
    
    // Message to -------------------------
        public class Controller2D : MonoBehaviour
        {
    	static public float horizontalMove = 0f;
    	void Update()
    	{
    		characterController.Move (moveDirection * Time.deltaTime);
    		horizontal = horizontalMove;
    		moveDirection.y -= gravity * Time.deltaTime;
    		
    		if (horizontal ==0)
    		{
    			moveDirection.x = horizontal;
    		}
    }

create a method in the object to receive the message:

void horizontalMove(float amount)
{
  //store or handle amount.
}

In the first object add gameobject field and set it to the receiving gameobject in the unity editor.

GameObject messageReceiver;

Finally, you can

messageReciever.sendMessage(horizontalMove, 1.0f, SendMessageOptions.DontRequireReceiver);

It’s that easy!