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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

SendMessage causing error in standalone build

Discussion in 'Scripting' started by boothj1985, Jan 22, 2016.

  1. boothj1985

    boothj1985

    Joined:
    Jul 15, 2012
    Posts:
    154
    I get this error "
    NullReferenceException
    at (wrapper managed-to-native) UnityEngine.GameObject:SendMessage (string,object,UnityEngine.SendMessageOptions)

    at UnityEngine.GameObject.SendMessage (System.String methodName) [0x00000] in <filename unknown>:0

    at ReportDistanceFromOther._sendMessage (System.String FunctionName) [0x00000] in <filename unknown>:0

    at ReportDistanceFromOther._SendMessagePre () [0x00000] in <filename unknown>:0

    at ReportDistanceFromOther.FixedUpdate () [0x00000] in <filename unknown>:0"

    in the output log and the frame rate is very low. The main script check's the players distance to the object and sends messages to itself and possibly near or connected objects to let them know they should cease certain unnecessary functions for performance optimization.
    Code (JavaScript):
    1. var MainObject : GameObject;
    2. var otherObject : GameObject;
    3. var _On : boolean = true;
    4. var _distance : float;
    5. var _RangeLimit : float = 100;
    6. var InRange : boolean;
    7. var _InRange_duplicate_variable : boolean;
    8. var autoAwake : boolean;
    9. var _viaMessage : boolean;
    10. var _ToSendMessageTo : GameObject[];
    11. var freezeIfOutOfRange : boolean;
    12. var LastInRangePos : Vector3;
    13. var _lastInRangePosRecorded : boolean;
    14. var refereceOther : GameObject;
    15. var _referredToScript : Component;
    16. private var firstCheck : boolean;
    17. private var PrevInRange : boolean;
    18. private var woke : boolean;
    19. private var mainObjTransform : Transform;
    20. private var OtherObjTransform : Transform;
    21.  
    22. function Awake()
    23. {
    24.     if(autoAwake == true)
    25.         _Awake();
    26. }
    27.  
    28. function _Awake()
    29. {
    30.     if(refereceOther != null)
    31.     {
    32.         _referredToScript = refereceOther.GetComponent("ReportDistanceFromOther");
    33.         return;
    34.     }
    35.     if(gameObject.GetComponent(Rigidbody) != null)
    36.         freezeIfOutOfRange = true;
    37.     if(_viaMessage == true)
    38.     {
    39.         if(_ToSendMessageTo.length == 0)
    40.         {          
    41.             var arr = new Array ();
    42.             arr.Push (gameObject);
    43.             _ToSendMessageTo = arr;
    44.         }
    45.         if(otherObject == null)
    46.             otherObject = GameObject.Find("HexController");
    47.     }
    48.     if(MainObject == null)
    49.         MainObject = gameObject;
    50.     mainObjTransform = MainObject.transform;
    51.     if(otherObject != null)
    52.         OtherObjTransform = otherObject.transform;
    53.     else
    54.         _On = false;
    55.     woke = true;
    56. }
    57.  
    58. function FixedUpdate ()
    59. {
    60.     if(woke == true)
    61.     {
    62.         if(_On == true)
    63.         {
    64.             _distance = Vector3.Distance(mainObjTransform.position,OtherObjTransform.position);
    65.             if(_distance <= _RangeLimit)
    66.             {
    67.                 if(_referredToScript == null)
    68.                 {
    69.                     InRange = true;
    70.                     _InRange_duplicate_variable = InRange;
    71.                 }
    72.                 else
    73.                 {
    74.                     InRange = _referredToScript._InRange_duplicate_variable;
    75.                     _InRange_duplicate_variable = InRange;
    76.                 }
    77.             }
    78.             else
    79.             {
    80.                 if(_referredToScript == null)
    81.                 {
    82.                     InRange = false;
    83.                     _InRange_duplicate_variable = InRange;
    84.                 }
    85.                 else
    86.                 {
    87.                     InRange = _referredToScript._InRange_duplicate_variable;
    88.                     _InRange_duplicate_variable = InRange;
    89.                 }
    90.                 if(freezeIfOutOfRange == true)
    91.                 {
    92.                     if(_lastInRangePosRecorded == true)
    93.                         transform.position = LastInRangePos;
    94.                 }
    95.             }
    96.             if(_viaMessage == true)
    97.             {
    98.                 if(InRange != PrevInRange)
    99.                     _SendMessagePre();
    100.                 if(firstCheck == false)
    101.                 {
    102.                     _SendMessagePre();
    103.                     firstCheck = true;
    104.                 }
    105.             }
    106.             PrevInRange = InRange;
    107.         }
    108.     }
    109. }
    110.  
    111. function _SendMessagePre()
    112. {
    113.     if(InRange == true)
    114.     {
    115.         _sendMessage("IsInRange");
    116.         if(freezeIfOutOfRange == true)
    117.             _lastInRangePosRecorded = false;
    118.     }
    119.     else
    120.     {
    121.         if(freezeIfOutOfRange == true)
    122.         {
    123.             if(_lastInRangePosRecorded == false)
    124.             {
    125.                 LastInRangePos = transform.position;
    126.                 _lastInRangePosRecorded = true;
    127.             }
    128.         }
    129.         _sendMessage("NotInRange");
    130.     }
    131. }
    132.  
    133. function _sendMessage(FunctionName : String)
    134. {
    135.     for(var i : int = 0;i<_ToSendMessageTo.length;i++)
    136.         _ToSendMessageTo[i].SendMessage(FunctionName);
    137. }
    I'm using Unity 3.4.2 btw.
     
  2. boothj1985

    boothj1985

    Joined:
    Jul 15, 2012
    Posts:
    154
    Nevermind, I found out what was wrong.
     
  3. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    It would be nice if you could provide a brief explanation of what was wrong, as it may be useful for other people reading this in the future.
     
  4. boothj1985

    boothj1985

    Joined:
    Jul 15, 2012
    Posts:
    154
    Using SendMessageOptions.RequireReceiver was the solution. I like your avatar btw.