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

Can't figure out why this SendMessage won't work...

Discussion in 'Scripting' started by Caelorum, Sep 26, 2014.

  1. Caelorum

    Caelorum

    Joined:
    Feb 6, 2014
    Posts:
    32
    So i'm trying to send a message between two scripts to activate a function.

    In my ball script, I have this function:

    Code (CSharp):
    1. public void ResetBall ()
    2.     {
    3.         var rigY = rigidbody2D.velocity.y;
    4.         var rigX = rigidbody2D.velocity.x;
    5.         var traY = transform.position.y;
    6.         var traX = transform.position.x;
    7.         rigY = 0f;
    8.         rigX = 0f;
    9.         traY = 0f;
    10.         traX = 0f;
    11.  
    12.         Invoke("goBall", 0.5f);
    13.     }
    This is used to reset the ball when it hits one of the outer edges.

    I'm using this script, on the wall, to access that function ResetBall()

    Code (CSharp):
    1. void OnTriggerEnter2D (Collider2D hitInfo)
    2.     {
    3.         if (hitInfo.name == "Ball")
    4.         {
    5.             var wallName = transform.name;
    6.             gameMan.Score (wallName);
    7.             hitInfo.gameObject.SendMessage ("ResetBall",SendMessageOptions.RequireReceiver);
    8.  
    9.  
    10.         }
    11.    
    12.     }
    I was assuming since hitInfo is technically linked to the ball once it hits the wall, that I could just send message over to the ResetBall function. But so far all I get is 4 warning on my rigY/X, traY/X variables, saying they are never used, and the ball just keeps flying away when it hits the "wall".

    Can anyone give me a hand please? I'm still relatively new to coding, and while I understand what to put where most of the time, the actual "knowledge" of coding is still hard for me. I can read it for the most part, but I can't really read into the logic most of the time.
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You should rather get a reference to the script and call it via the reference. SendMessage, to my knowledge, acts as a kind of broadcast system which is not neccessary in this case.

    Try
    Code (CSharp):
    1. if (hitInfo.name == "Ball")
    2.         {
    3.             var wallName = transform.name;
    4.             gameMan.Score(wallName);
    5.             hitInfo.GetComponent<YourClass>().ResetBall();
    6.         }
    'YourClass' has to be replaced with the class' name that holds the ResetBall method.
     
    Caelorum likes this.
  3. Caelorum

    Caelorum

    Joined:
    Feb 6, 2014
    Posts:
    32

    Well it runs. And thank you for the alternate code by the way, didn't know I could do it that easily. But the ball still won't reset back to it's neutral position. It can't be anything wrong with the ResetBall script can it? All i'm doing is setting it's x and y positions back to zero, and stopping it's velocity.. It's still giving me warnings saying none of those variables are being used. Which I don't understand.
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Oh, i didn't look at that script.
    The problem is that you assign data to data type variables.
    E.g. an integer stores the data and copying it will... copy it. Any changes done to the copy won't effect the original, while 'copying' reference types such as classes will effect the original, because you will only pass references around which all point to the same 'real' instance and its data.

    Or short:
    Code (CSharp):
    1. rigidbody2D.velocity = Vector2.zero;
    2. transform.position = new Vector3(0,0,0);
    You have assign the values to the properties directly. Note, that you cannot set the axis individually, that's why we use a whole Vector2/Vector3.
    I can't be sure that everything works as expected now, since i don't know your scene setup and if there are any objects at 0,0,0 which would cause any physical reaction.
     
  5. Caelorum

    Caelorum

    Joined:
    Feb 6, 2014
    Posts:
    32
    Wow that was such a small piece of code and it worked. Thank you very much.

    But this is my problem. I can't understand the meaning of code. I see vector2 and all I think of is movement. I don't understand the whole references/copies thing. I can't fix any of my errors because I don't know WHY code does things.

    Mind if I ask how you learned your coding skill?
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    About vectors:
    There's alot more you can do with vectors. Basically, they describe a position in a n-dimensional space.
    But you can also describe directions, distances etc by a bit of math. It's really neccessary to understand them, it will simplify your life a lot.

    About coding, long story short:

    Informatics school, you should know that i gave it several shots the years before and i never understood what was going on (i had tried some books before i really learned coding, but i was too lazy and the stuff they try to teach is often pretty boring).

    Anyway, at school i was learning C++ for about 2 years. It's a really nice programming language.
    I had a great & funny programming teacher and it's really important that you have fun during learning phases. Also, i learned a bit of programming a micro controller with assembler (later on C).
    As i was interested in Unity for a long time before i even started to learn coding, i finally got into to C# and Java (and a bit of JavaScript/Unity Script but i never use JS/US) which was very confusing in the beginning because there are a few yet fundamental differences.

    Sorry for any typos/ wrong tenses. Not my native language and pretty tired.