Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Still incredibly frustrated by Messages

Discussion in 'Scripting' started by wilhelmscream, Jan 7, 2014.

  1. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    I'm just about on the verge of giving up on all this after weeks of trying to solve one problem..... so.... last shot at this.....

    I have 2 objects - Small Frame and Large Frame. Renderer on Large Frame is disabled. Clicking on Small Frame should make Large Frame visible. Large Frame is the direct child of Small Frame. I've tried this two different ways.....

    Option A:

    Code for Small Frame:

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. // this works fine
    5. function Start () {
    6.     renderer.enabled = false;
    7. }
    8.  
    9. //as does this
    10. function OnMouseEnter () {
    11.     renderer.enabled = true;
    12. }
    13.  
    14. //this is the problem
    15. function OnMouseDown () {
    16.     BroadcastMessage ("ShowPlanets");
    17. }
    18.  
    19. //though this is fine too
    20. function OnMouseExit () {
    21.     renderer.enabled = false;
    22. }
    23.  
    Here is the code for Large Frame

    Code (csharp):
    1.  
    2.  
    3. renderer.enabled = false;
    4.  
    5. function Start () {
    6. }
    7.  
    8. function OnShowPlanets () {
    9.     renderer.enabled = true;
    10. }
    11.  
    And when clicking on Small Frame I still get a message telling me ShowPlanets has no receiver.



    Option B:

    Here is the other code I've tried using for Small Frame:

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var largeFrame : GameObject;
    5.  
    6. // this works fine
    7. function Start () {
    8.     renderer.enabled = false;
    9. }
    10.  
    11. //as does this
    12. function OnMouseEnter () {
    13.     renderer.enabled = true;
    14. }
    15.  
    16. //this is the problem
    17. function OnMouseDown () {
    18.     BroadcastMessage ("ShowPlanets", largeFrame, SendMessageOptions.DontRequireReceiver);
    19. }
    20.  
    21. //though this is fine too
    22. function OnMouseExit () {
    23.     renderer.enabled = false;
    24. }
    25.  
    Doing that - after double checking I have the proper game object selected in the inspector - results in no error message..... and in fact, absolutely nothing happening when I click on Small Frame.


    Like I said, I'm about done with this so I hope this can finally get solved.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You're using "ShowPlanets" in BroadcastMessage but you have no function called that.

    --Eric
     
  3. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    That was a transcription error on my part due to typing rather than copy/pasting. it actually say

    Code (csharp):
    1.  
    2. function ShowPlanets () {
    3.  
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    With the function name fixed, the only way the situation you described in Option A wouldn't work is if the Large Frame object was inactive (rather than just having the renderer disabled).

    --Eric
     
  5. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    I checked that too and as far as I can tell it's active.

    The alternative (which I actually preferred) was to use GUI rather than an object for Large Frame, but the other objects involved (planets) were then obscured. So I'm left trying to fix this.
     
  6. Brian-Stone

    Brian-Stone

    Joined:
    Jun 9, 2012
    Posts:
    222
    Your ShowPlanets function doesn't take any arguments, but you try to send it a GameObject called "largeFrame" in BroadcastMessage.

    Code (csharp):
    1.  
    2. var largeFrame : GameObject;
    3.  
    4. function OnMouseDown () {
    5.  
    6.     BroadcastMessage ("ShowPlanets", largeFrame, SendMessageOptions.DontRequireReceiver);
    7.  
    8. }
    Therefore, ShowPlanets must accept a GameObject in its parameter list.

    Code (csharp):
    1.  
    2. function ShowPlanets(frame : GameObject) {
    3. }
    4.  
     
  7. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    That's something new to try at least. Let me see what happens.
     
  8. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    I just tried this and it works:

    from an empty scene I added 1 cube called smallframe position 0,0,0 scale 1,1,1
    attach this script to it smallframe.js
    Code (csharp):
    1.    #pragma strict
    2.      
    3.     function OnMouseDown () {
    4.         BroadcastMessage ("ShowPlanets");
    5.     }
    added another cube to the scene and made it a child of the first cube, called it largeframe set position to 2,0,0 and scale to 2,2,2 and attached this script to it largeframe.js

    Code (csharp):
    1.     renderer.enabled = false;
    2.      
    3.     function ShowPlanets () {
    4.         renderer.enabled = true;
    5.     }
    press play, click the small cube and the large on appears.

    Heres the package
    View attachment $smallframe-largeframe.unitypackage
     
    Last edited: Jan 7, 2014