Search Unity

Problems with broadcasting and reciever

Discussion in 'Scripting' started by MrAwesome, Nov 2, 2013.

  1. MrAwesome

    MrAwesome

    Joined:
    Sep 1, 2011
    Posts:
    171
    Hello I just started up on Unity agian, and I have walked into som problems

    This is the script there send the message "ApplyDamage" when input.fire1 have been pressed:

    Code (csharp):
    1.         var fireRate : float = 0.5;
    2. private var nextFire : float = 0.0;
    3.  
    4.  
    5. function Update ()
    6. {
    7.     if (Input.GetButton ("Fire1")  Time.time > nextFire) {
    8.         nextFire = Time.time + fireRate;
    9.         BroadcastMessage ("ApplyDamage");
    10.         }
    11.     }
    12.  
    13.  
    14.  

    This is the the game object there was planned to recieve the message


    Code (csharp):
    1.  
    2.  
    3. var Player : GameObject;
    4. Player = GameObject.Find("Player");
    5. var distance : float;
    6.  
    7.  
    8. function Update ()
    9. {
    10.     distance = Vector3.Distance(transform.position, Player.transform.position);
    11. }
    12.  
    13.  
    14. function ApplyDamage (){
    15.  
    16.  
    17.     if(distance < 20){
    18.         Debug.Log("ramt");
    19.         Destroy (gameObject);  
    20. }
    21.  
    22. }

    This is 2 different object, and i get the error:
    BroadcastMessage ApplyDamage has no receiver!
    UnityEngine.Component:BroadcastMessage(String)
     
    Last edited: Nov 2, 2013
  2. Kirlim

    Kirlim

    Joined:
    Aug 6, 2012
    Posts:
    126
    I don't really understand javascript, but hte "no receiver" error message appears when the object receiving the broadcast does not have any active component (or the object is disabled) to receive the message. Also... the code looks strange. I mean, where are you specifying who to broadcast the message? It looks more like you are broadcasting the message to the sending object itself.
     
  3. MrAwesome

    MrAwesome

    Joined:
    Sep 1, 2011
    Posts:
    171
    The first script is at Object A

    The second script is at Object B


    Kirlim, the plan is that Object A is player, when it hit Fire1 which is Attack, it sends out the message.

    Objebt B is the enemy and the reciever, and when it gets the "ApplyDamage" message it will check how far it is from the Object A before it destroys
     
    Last edited: Nov 2, 2013
  4. MrAwesome

    MrAwesome

    Joined:
    Sep 1, 2011
    Posts:
    171
    I found the solution

    The player who shall attack have the script

    Code (csharp):
    1.        
    2.         var fireRate : float = 0.5;
    3. private var nextFire : float = 0.0;
    4.         var Enemy : GameObject[]; // Making the variable to be an array
    5.  
    6.  
    7. function Update ()
    8. {
    9.     nextFire = Time.time + fireRate; // Decides Rate of fire
    10.    
    11.     if (Input.GetButton ("Fire1")  Time.time > nextFire) {
    12.     SendNewMessage(); //Calling the function
    13.     }
    14. }
    15.  
    16. function SendNewMessage(){
    17.        
    18.         // Define Enemy with all Gameobjects with tag "Enemy"
    19.         Enemy = GameObject.FindGameObjectsWithTag("Enemy");
    20.        
    21.         //List up all the enemy it could find, and then send it.
    22.         for(var i = 0; i<Enemy.length; i++){
    23.             Enemy[i].SendMessage("ApplyDamage");
    24.        
    25.         }
    26.    
    27.  
    28. }
    And the enemy have this script:

    Code (csharp):
    1.  
    2.  
    3. var Player : GameObject;
    4. Player = GameObject.Find("Player");
    5. var distance : float;
    6.  
    7.  
    8. function Update ()
    9. {
    10.     distance = Vector3.Distance(transform.position, Player.transform.position);
    11. }
    12.  
    13.  
    14. function ApplyDamage (){
    15.  
    16.  
    17.     if(distance < 20){
    18.         Debug.Log("ramt");
    19.         Destroy (gameObject);  
    20. }
    21.  
    22. }