Author Topic: Exception has been thrown by the target of an invocation.  (Read 9802 times)

Voxel

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 35
    • View Profile
Exception has been thrown by the target of an invocation.
« on: August 11, 2013, 03:09:36 PM »
Hi I know this is an open ended question but I bump into this error a fair bit. What exactly does it mean?

For example today I have this error in a script that makes the chat window open/close on all clients by any client who wants to start it:
Exception has been thrown by the target of an invocation.
chatWindowToggle.ChatMode (System.Boolean)

Below is both scripts. They are both on the same gameobject....


Here is the script @RFC:
  1. #pragma strict
  2.  
  3. var chatScript : ExampleChat;
  4. var playerMoveScript : playerPhysixMoveAim;
  5. var playerShootScript : playerShootV2;
  6. var chatModeEnabled : boolean;
  7.  
  8. var tno : TNObject;
  9.  
  10. //-------------------------------------------------------------------------------------------------------
  11. //-------------------------------------------------------------------------------------------------------
  12.  
  13. function Awake (){
  14.     if(!TNManager.isThisMyObject) {
  15.           Destroy (this);
  16.     }
  17. }
  18.  
  19. //-------------------------------------------------------------------------------------------------------
  20. //-------------------------------------------------------------------------------------------------------
  21.  
  22. function Start () {
  23.  
  24.     chatScript = GameObject.Find("--Level--/GUI/Chat").GetComponent(ExampleChat);
  25.         playerMoveScript = GetComponent(playerPhysixMoveAim);
  26.         playerShootScript = GetComponent(playerShootV2);
  27.         chatScript.enabled = false;
  28.         tno = GetComponent(TNObject);
  29.        
  30. }
  31.  
  32. //-------------------------------------------------------------------------------------------------------
  33. //-------------------------------------------------------------------------------------------------------
  34.  
  35. function Update () {
  36.  
  37.                 if(Input.GetKeyDown(KeyCode.Escape)){
  38.                         chatScript.enabled = !chatScript.enabled;
  39.                         playerMoveScript.enabled = !playerMoveScript.enabled;
  40.                         playerShootScript.enabled = !playerShootScript.enabled;
  41.                         chatModeEnabled = !chatModeEnabled;
  42.                         tno.Send("ChatMode", Target.Others, chatModeEnabled);
  43.                 }
  44. }
  45.  
  46. //-------------------------------------------------------------------------------------------------------
  47. //-------------------------------------------------------------------------------------------------------
  48.  
  49. @RFC
  50. function ChatMode (chatModeActive : boolean){
  51.                 print("TNO RECIEVED" + chatModeActive);
  52.                 if(chatModeActive == true){
  53.                         chatScript.enabled = true;
  54.                         playerMoveScript.enabled = false;
  55.                         playerShootScript.enabled = false;
  56.                 }
  57.                
  58.                 if(chatModeActive == false){
  59.                         chatScript.enabled = false;
  60.                         playerMoveScript.enabled = true;
  61.                         playerShootScript.enabled = true;
  62.                 }
  63. }

What am I doing wrong?
« Last Edit: August 12, 2013, 03:43:58 PM by Voxel »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Exception has been thrown by the target of an invocation.
« Reply #1 on: August 11, 2013, 03:57:47 PM »
Means you got a null exception inside your function.

So for example if "chatScript" is "null" then you will get this error because you never check for null before using the values.

Voxel

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 35
    • View Profile
Re: Exception has been thrown by the target of an invocation.
« Reply #2 on: August 12, 2013, 02:07:19 PM »
Yep, spot on, that is exactly what I found, but it doesn't make sense. So I changed the script to be dedicated as an @RFC for simplicity/debug purposes. Here is the script:

Here is the 'Trigger Script':

  1. #pragma strict
  2.  
  3. var chatScript : ExampleChat;
  4. var playerMoveScript : playerPhysixMoveAim;
  5. var playerShootScript : playerShootV2;
  6. var chatModeEnabled : boolean;
  7.  
  8. var tno : TNObject;
  9.  
  10. //-------------------------------------------------------------------------------------------------------
  11. //-------------------------------------------------------------------------------------------------------
  12.  
  13. function Awake (){
  14.     if(!TNManager.isThisMyObject) {
  15.           Destroy (this);
  16.     }
  17. }
  18.  
  19. //-------------------------------------------------------------------------------------------------------
  20. //-------------------------------------------------------------------------------------------------------
  21.  
  22. function Start () {
  23.  
  24.     chatScript = GameObject.Find("--Level--/GUI/Chat").GetComponent(ExampleChat);
  25.         playerMoveScript = GetComponent(playerPhysixMoveAim);
  26.         playerShootScript = GetComponent(playerShootV2);
  27.         chatScript.enabled = false;
  28.         tno = GetComponent(TNObject);
  29.        
  30. }
  31.  
  32. //-------------------------------------------------------------------------------------------------------
  33. //-------------------------------------------------------------------------------------------------------
  34.  
  35. function Update () {
  36.  
  37.                 if(Input.GetKeyDown(KeyCode.Escape)){
  38.                         chatScript.enabled = !chatScript.enabled;
  39.                         playerMoveScript.enabled = !playerMoveScript.enabled;
  40.                         playerShootScript.enabled = !playerShootScript.enabled;
  41.                         chatModeEnabled = !chatModeEnabled;
  42.                         tno.Send("ChatMode", Target.Others, chatModeEnabled);
  43.                 }
  44. }
  45.  
  46. //-------------------------------------------------------------------------------------------------------
  47. //-------------------------------------------------------------------------------------------------------
  48.  




  1. #pragma strict
  2.  
  3. var chatScript : ExampleChat;
  4. var playerMoveScript : playerPhysixMoveAim;
  5. var playerShootScript : playerShootV2;
  6.  
  7. var test : int;
  8. //-------------------------------------------------------------------------------------------------------
  9. //-------------------------------------------------------------------------------------------------------
  10.  
  11. function Awake () {
  12.  
  13.     chatScript = GameObject.Find("--Level--/GUI/Chat").GetComponent(ExampleChat);
  14.         playerMoveScript = GetComponent(playerPhysixMoveAim);
  15.         playerShootScript = GetComponent(playerShootV2);
  16. }
  17.  
  18. //-------------------------------------------------------------------------------------------------------
  19. //-------------------------------------------------------------------------------------------------------
  20.  
  21. @RFC
  22. function ChatMode (chatModeActive : boolean){
  23.                 print("TNO RECIEVED" + chatModeActive);
  24.                
  25.                 print(chatModeActive);
  26.                 print(playerMoveScript);
  27.                 print(playerShootScript);
  28.                
  29.                 if(chatModeActive == true){
  30.                         if(chatScript){
  31.                                 chatScript.enabled = true;
  32.                         }
  33.                         if(playerMoveScript){
  34.                                 playerMoveScript.enabled = false;
  35.                         }
  36.                         if(playerShootScript){
  37.                                 playerShootScript.enabled = false;
  38.                         }
  39.                        
  40.                         test += 1;
  41.                        
  42.                 }
  43.                
  44.                 if(chatModeActive == false){
  45.                         if(chatScript){
  46.                                 chatScript.enabled = false;
  47.                         }
  48.                         if(playerMoveScript){
  49.                                 playerMoveScript.enabled = true;
  50.                         }
  51.                         if(playerShootScript){
  52.                                 playerShootScript.enabled = true;
  53.                         }
  54.                        
  55.                         test -= 1;
  56.                 }
  57. }

Now here is the weird bit. Looking at the script in the Unity Inspector, all scripts show as found, yet the print statements show as null for the playerMoveScript, and the playerShootScript. The script works without the "Exception has been thrown by the target of an invocation" error because of the if statements.

So why when the scripts are shown as found am I getting a null from the @RFC function?
If I call ChatMode(true); in function Start it works as it should. Is there something I am not understanding about TNet or multiplayer networking in general?
« Last Edit: August 12, 2013, 04:05:02 PM by Voxel »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Exception has been thrown by the target of an invocation.
« Reply #3 on: August 12, 2013, 03:22:47 PM »
As is usual in such things, it's a matter of the order of execution.

The Start() function can be called *after* the RFCs -- and in fact when joining an existing channel, that's usually the case. You need to set your variable initialization in either Awake or OnEnable. It's often better to have it be set in inspector instead so that you don't have to do any of this.

Voxel

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 35
    • View Profile
Re: Exception has been thrown by the target of an invocation.
« Reply #4 on: August 12, 2013, 03:42:56 PM »
Hmmm, I've been down that route and just checked again. The variable initialization has been set in both Awake and OnEnable. If have also set them in the inspector and I get the same results in all cases. I have also tried getcomponent in the ChatMode function, but still no luck.

To explain further the chat window is not opened on the start of the game. It is called when a player presses the Escape key to start a chat.

To test further I have added a simple increase/decrease int, this also fails to do anything when the function is called as @RFC, but works fine when called in the start function. I have updated this script in my previous post.

I have also added to my previous post the trigger script as well (just in case!)

I feel I am going mad Arenmook! Or am I just being a bit stupid? ;)
« Last Edit: August 12, 2013, 03:57:34 PM by Voxel »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Exception has been thrown by the target of an invocation.
« Reply #5 on: August 12, 2013, 04:10:29 PM »
Wait wait... chat window is not open... does it mean that it's disabled? As in, the entire game object is disabled?

Voxel

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 35
    • View Profile
Re: Exception has been thrown by the target of an invocation.
« Reply #6 on: August 12, 2013, 04:59:23 PM »
No. Just the component gets enabled/disabled.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Exception has been thrown by the target of an invocation.
« Reply #7 on: August 13, 2013, 07:40:01 PM »
TNObject should stay enabled. You can turn off children of that game object, or other scripts, but you should not turn off TNObjects. RFCs sent to a disabled TNObject will not work. Even if they do work, the script's initialization will not have run, so the references will not be set.

Voxel

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 35
    • View Profile
Re: Exception has been thrown by the target of an invocation.
« Reply #8 on: August 14, 2013, 07:07:05 AM »
TNObject is not being disabled / enabled at any point, the only objects being enabled /disabled are in the script examples I posted.
Let's focus on the RFC situation:

The @RFC is always called successfully.

This prints as it should when the @RFC is called:
print("TNO RECIEVED" + chatModeActive);

This does nothing at all even the the above works:
test += 1;

How can this super simple increase of an int value fail? I am also going to try some other testing tonight out of desperation.....

Update: Still testing and if I have just this as the @RFC:
  1. @RFC
  2. function ChatMode (chatModeActive : boolean){
  3.         print("TNO RECIEVED" + chatModeActive); // <<This works!
  4.         print("This is attached to: " + this.gameObject);// << Exception has been thrown by the target of an invocation error
  5. }
  6.  

Not sure if that could be a clue? I'm really trying to get to the bottom of this Arenmook, sorry to be a pain :(
« Last Edit: August 14, 2013, 01:39:18 PM by Voxel »

Voxel

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 35
    • View Profile
Re: Exception has been thrown by the target of an invocation.
« Reply #9 on: August 14, 2013, 03:03:20 PM »
OK, it's fixed, but I had to move scripts around and rework it. There must be something I am not understanding *scratches head*

What I did was to move the chat toggle scripts to a gameobject that is not Created by TNManager. That gameobject also contains the example chat script.

The scripts were located on the player avatar that is created by  TNManager.Create on start. Could that be the cause?

If you can shed some light that would be great but I know your are busy Arenmook and I appreciate the help you have already given, so don't worry too much! Thanks :)
« Last Edit: August 14, 2013, 04:06:44 PM by Voxel »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Exception has been thrown by the target of an invocation.
« Reply #10 on: August 14, 2013, 11:00:45 PM »
Yes, like I said earlier -- matter of script execution. Scripts may not be present at the time of creation and/or initialization.