SendMessage won't work..

Why my SendMessage give me the error : SendMessage has no receiver!, Why ? plz… i need example :slight_smile: Thanks!

Script 1 (Sender)

#pragma strict

var speed = 13;
var boostSpeed = 30;
var jumpSpeed = 8.0;
var gravity = 20.0;

private var moveDirection = Vector3.zero;
private var grounded : boolean = false;

function Start () {

}
function FixedUpdate() {
    if (grounded) {

       moveDirection = Vector3(Input.GetAxis("HorizontalP1"), 0, Input.GetAxis("VerticalP1"));
       moveDirection = transform.TransformDirection(moveDirection);
       moveDirection *= speed;

       if (Input.GetButton ("JumpP1")) {
         moveDirection.y = jumpSpeed;
       }
    }
    moveDirection.y -= gravity * Time.deltaTime;

    var controller : CharacterController = GetComponent(CharacterController);
    var flags = controller.Move(moveDirection * Time.deltaTime);
    grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}

function OnTriggerEnter (other : Collider){
	if (other.name == ("Diamond")) {
		SendMessage ("takeDiamond");
		Destroy(other.gameObject);
		}
	if (other.name == ("SuperDiamond")){
		SendMessage ("takeSuperDiamond");
		Destroy(other.gameObject);
		}
}

@script RequireComponent(CharacterController)

Script 2 (Receiver)
#pragma strict

var scoreValue : float;
var Player1Score : TextMesh;
var diamondScore = 10;
var superDiamondScore = 100;
var scoreString = "Player 1 : 0";

private var score = 0;

public var SpawnPoint1_1 : GameObject;
public var SpawnPoint1_2 : GameObject;

function Start () {

}

function Update () {
	Player1Score.text = "Player 1 : " + score;
}

function takeDiamond(){
	score += diamondScore;
    scoreValue += diamondScore;
    scoreString = "Player 1 : "+scoreValue;
}

function takeSuperDiamond() {
   score += superDiamondScore;
   scoreValue += superDiamondScore;
   scoreString = "Player 1 : "+scoreValue;
}

Your code:

function OnCollisionEnter(col : Collision) {
    if (col.gameObject.name == ("Diamond")) {
       SendMessage ("takeDiamond");
       Destroy(other.gameObject);
       }
    if (col.gameObject.name == ("SuperDiamond")){
       SendMessage ("takeSuperDiamond");
       Destroy(other.gameObject);
       }
}

What you are doing now, is sending message to the object that has Script1 attached (e.g. sender itself). If your script2 is on some other object you need to have its reference. Then:

reciever.SendMessage("functionName"); //reviever is object of type GameObject

EDIT: I see you asked practically identical question a month ago.