Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[C#] Having trouble using SendMessageUpwards

Discussion in 'Scripting' started by slimabob, Jan 15, 2015.

  1. slimabob

    slimabob

    Joined:
    Jul 7, 2013
    Posts:
    23
    So basically, what I have is a model made up of several different meshes and so that means I also have a few separate colliders, and all of the meshes are parented to a single empty Gameobject that holds everything together and contains all the scripts. Here's an image to give you an idea of what I'm talking about:


    Now I figured that I could put a SendMessageUpwards onto my projectile so that when it hit any part of the ship, it would send ApplyDamage to the Gameobject with the script on it. Am I doing this wrong? Am I using ApplyDamage incorrectly? I'm still a relatively novice programmer, so excuse my organization on the following scripts:

    The ProjectileManager.cs (Goes onto the projectile itself. The IENumerator is to destroy the projectile after a while so that there aren't hundreds of irrelevant projectiles flying around in the void):
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ProjectileManager : MonoBehaviour {
    5.  
    6.     public float dmg = 25.0f;
    7.    
    8.     void Start () {
    9.         StartCoroutine(destroyaftertime());
    10.     }
    11.  
    12.     void OnCollisionEnter(Collision collision){
    13.         Destroy(gameObject);
    14.         Debug.Log("hit!");
    15.  
    16.         collider.SendMessageUpwards("Damage",dmg,SendMessageOptions.DontRequireReceiver);
    17.     }
    18.  
    19.     IEnumerator destroyaftertime(){
    20.         yield return null;
    21.         yield return new WaitForSeconds(10);
    22.         Destroy(gameObject);
    23.     }
    24. }
    25.  
    And this is my ShipAttributes.cs (This goes on the parent of all the ship parts):
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ShipAttributes : MonoBehaviour {
    5.  
    6.     public float Health = 100.0f;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.         if(Health <=0.0f){
    16.             Destroy(gameObject);
    17.         }
    18.    
    19.     }
    20.  
    21.     void ApplyDamage(float dmg){
    22.         Debug.Log("OUCH!");
    23.         Health -= dmg;
    24.     }
    25. }
    Also, I am using this with Photon, so I am instantiating the players, if that makes a difference. Here is how I'm going about that:

    Code (CSharp):
    1.     void SpawnMyPlayer() {
    2.         SpawnSpot mySpawnSpot = spawnSpots[Random.Range(0, spawnSpots.Length)];
    3.         GameObject myPlayer = (GameObject)PhotonNetwork.Instantiate("FighterShip", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation,0);
    There's more there in SpawnMyPlayer, but it's just enabling scripts on the player, and I've already made sure that ShipAttributes is enabled when the player is created.

    Help would be appreciated, I'm stumped!
     
  2. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    This may or may not help you, but you should really consider using events instead of SendMessage; events are both faster and easier to work with in the long run.
     
    Kiwasi likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You are sending the message Damage. You are trying to receive ApplyDamage. These two should match.
     
  4. slimabob

    slimabob

    Joined:
    Jul 7, 2013
    Posts:
    23
    Never noticed this, I'll have to fix that when I get back. Cheers
     
  5. slimabob

    slimabob

    Joined:
    Jul 7, 2013
    Posts:
    23
    Update: Changing to ApplyDamage did not fix the problem, so there is another issue.

    I will look into events, thanks for the heads up