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

Why is my send message not working?

Discussion in 'Scripting' started by epochplus5, Feb 11, 2021.

  1. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    I have an asteroid script that has different size asteroids, and I want them to damage my space station according to size, so large is 20 off the armour, medium is 10 and small is 5.

    so im trying to get my different size asteroids send a message to the space staion script to minus the appropriate armour when hit.

    on the asteroid script i have:

    Code (CSharp):
    1.  public GameObject newGrange;
    2.  
    3. private int asteroidSizeDamage;
    4.  
    5. void Start()   {
    6.     newGrange = GameObject.FindWithTag("NewGrange");
    7. }
    8.  
    9. void OnTriggerEnter2D(Collider2D other)
    10.     {
    11.         //Check to see if bullet
    12.         if (other.CompareTag("PlayerLaser"))
    13.         {
    14.             //Debug.Log("Hit by:" + other.name);    to print what collided with your object
    15.             Destroy(other.gameObject);
    16.             //check asteroid size, spawn in next smaller size
    17.             if (asteroidSize == 3)
    18.             {
    19.                 asteroidSizeDamage = 20;
    20.                 newGrange.SendMessage("DamageByAsteroids", asteroidSizeDamage);
    21.              }
    22.       }
    23.  
    24.  
    Then on my other script, the space station one i have a function:

    Code (CSharp):
    1. void DamageByAsteroids(int damageByAsteroids)
    2.     {
    3.         stationArmour -= damageByAsteroids;
    4.         Debug.Log(damageByAsteroids);
    5.     }
    but why is DamageByAsteroids greyed out like its not been used?

    also, its not printing.
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    nobody knows what your .SendMessage function does :rolleyes:

    and also , there is no "SendMessage" function in the GameObject API, am i missing something :eek:
     
  3. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
  4. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    I would recommend to get the component and call it over there
     
    epochplus5 likes this.
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    Because it's not being used. At least, as far as the compiler is aware it isn't. SendMessage is an archaic and unreliable way of calling methods. Why not just use
    GetComponent<YourComponent>().DamageByAsteroids(asteroidSizeDamage);
    ?

    Then the compiler will know about your method being called.
     
    Bunny83, Joe-Censored and epochplus5 like this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
  7. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    Ok thanks all!!! Ha Ha Kurt yes, you have talked about interfaces. It freaked me out a bit. Will look at it again, and again, and again, and again.
     
    Kurt-Dekker likes this.
  8. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,446
    That's the right attitude ^^. Just learn your tools to be able to decide when and how to use them.
     
    Kurt-Dekker likes this.
  9. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    Note that your method has to be public for GetComponent<> to be able to call it. (For SendMessage it doesn't.)
    Regardless, your method should be calling and the Debug.Log should be printing, not sure what's up.
     
    Bunny83 likes this.