Author Topic: Trying to create a new object but get "Failed to call function"  (Read 228 times)

acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 8
  • -Receive: 0
  • Posts: 26
    • View Profile
As the title states , Im trying to create a new object using:

  1. Vector3 pos = new Vector3 (0, 0, 0);
  2. Quaternion rot = Quaternion.Euler(new Vector3(0, 0, 0));
  3. String playername = TNManager.GetPlayerData<string> ("CharacterName");
  4. TNManager.Instantiate (3, "CreatePlayer", playerprefabPath, false, pos, rot, playername);
  5.  

I passed all the arguments(rot, pos, playername, but still getting this error....

[TNet] Failed to call function CreatePlayer: parameters do not match signature
  Expected args: UnityEngine.GameObject, UnityEngine.Vector3, UnityEngine.Quaternion
  Received args: UnityEngine.GameObject, UnityEngine.Vector3, UnityEngine.Quaternion, System.String

Does anyone have an idea on whats wrong?

mythikos

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 15
    • View Profile
Re: Trying to create a new object but get "Failed to call function"
« Reply #1 on: April 17, 2017, 01:32:13 PM »
Without providing the CreatePlayer method, it is a bit hard to tell the issue. Based on what the error is saying, the CreatePlayer method you have defined is not equipped to take a System.String as an argument. My assumption is that your CreatePlayer method looks something like:
  1. public void CreatePlayer(GameObject obj, Vector3 pos, Quarternion rot) { }
when it should look like
  1. public void CreatePlayer(GameObject obj, Vector3 pos, Quarternion rot, string playerName) { }
If this is not the case, please provide the CreatePlayer method you have defined. Hope that helps.



Additional note (that is purely from the perfectionist programmer in me):
You are using
  1. String playername;
when it is generally recommended to use
  1. string playername;
string is an alias in c# for System.String so while what you made is technically not wrong, it is not conventional. it is a general convention to use 'string' for objects and 'String' to refer to the class itself (including its member methods).

acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 8
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: Trying to create a new object but get "Failed to call function"
« Reply #2 on: April 17, 2017, 03:36:03 PM »
Here is the createplayer method
  1. [RCC]
  2.         static GameObject CreatePlayer (GameObject prefab, Vector3 pos, Quaternion rot, string pn)
  3.         {
  4.                 // Instantiate the prefab
  5.                 GameObject go = prefab.Instantiate();
  6.  
  7.                 // Set the position and rotation based on the passed values
  8.                 Transform t = go.transform;
  9.                 t.position = pos;
  10.                 t.rotation = rot;
  11.                 go.name = pn;
  12.                 return go;
  13.  
  14.         }

acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 8
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: Trying to create a new object but get "Failed to call function"
« Reply #3 on: April 17, 2017, 04:05:14 PM »
I changed the
  1. String playername;
to
  1. string playername;
same error....

(btw ...my goal make the created object have the name of the logged in user - in this case "CharacterName")
The complete code is :
  1. private void OnJoinChannel(int channelID, bool success, string message)
  2.         {
  3.                 Debug.Log (string.Format ("network_onJoinChannel: channelID={0} success={1} message={2}", channelID, success, message));
  4.  
  5.                 Vector3 pos = new Vector3 (0, 0, 0);
  6.                 Quaternion rot = Quaternion.Euler(new Vector3(0, 0, 0));
  7.                 string playername = TNManager.GetPlayerData<string> ("CharacterName")  + tno.ownerID;
  8.  
  9.  
  10.                 if (manager_instance.isinsetup) { //Join lobby
  11.                         TNManager.Instantiate (1, "CreatePlayer", playerprefabPath, false, pos, rot, playername);
  12.                 }
  13.                 if (manager_instance.isinlobby) { //Join lobby
  14.                         TNManager.Instantiate (2, "CreatePlayer", playerprefabPath, false, pos, playername);
  15.                 }
  16.                 if (manager_instance.isinworld) { //Join World
  17.                         TNManager.Instantiate (3, "CreatePlayer", playerprefabPath, false, pos, playername);
  18.                 }
  19.  
  20.  
  21.         }
  22.  
  23.  
  24.         [RCC]
  25.         static GameObject CreatePlayer (GameObject prefab, Vector3 pos, Quaternion rot, string pn)
  26.         {
  27.                 // Instantiate the prefab
  28.                 GameObject go = prefab.Instantiate();
  29.  
  30.                 // Set the position and rotation based on the passed values
  31.                 Transform t = go.transform;
  32.                 t.position = pos;
  33.                 t.rotation = rot;
  34.                 go.name = pn;
  35.                 return go;
  36.  
  37.         }
  38.  


acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 8
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: Trying to create a new object but get "Failed to call function"
« Reply #4 on: April 17, 2017, 04:26:27 PM »

      
  1. if (manager_instance.isinsetup) { //Join lobby
  2.                         TNManager.Instantiate (1, "CreatePlayer", playerprefabPath, false, pos, rot, playername);
  3.                 }
  4.                 if (manager_instance.isinlobby) { //Join lobby
  5.                         TNManager.Instantiate (2, "CreatePlayer", playerprefabPath, false, pos, playername);
  6.                 }
  7.                 if (manager_instance.isinworld) { //Join World
  8.                         TNManager.Instantiate (3, "CreatePlayer", playerprefabPath, false, pos, playername);
  9.                 }
  10.  
  11.  
  12.         }
  13.  

should be
  1. if (manager_instance.isinsetup) { //Join lobby
  2.                         TNManager.Instantiate (1, "CreatePlayer", playerprefabPath, false, pos, rot, playername);
  3.                 }
  4.                 if (manager_instance.isinlobby) { //Join lobby
  5.                         TNManager.Instantiate (2, "CreatePlayer", playerprefabPath, false, pos, rot, playername);
  6.                 }
  7.                 if (manager_instance.isinworld) { //Join World
  8.                         TNManager.Instantiate (3, "CreatePlayer", playerprefabPath, false, pos, rot, playername);
  9.                 }

but same error

mythikos

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 15
    • View Profile
Re: Trying to create a new object but get "Failed to call function"
« Reply #5 on: April 17, 2017, 06:25:25 PM »
Without testing it (which I cant at the moment, currently at work), its a bit hard to give you a definitive answer. However, I wonder if there are two method declarations for CreatePlayer (I recall there being one in an example for TNet) and if that is the case, I wonder if TNManager.Instantiate is picking the wrong method to call. Try calling the method by a defined rccid instead of by the func name. Something like the below:

 
  1.     private void OnJoinChannel(int channelID, bool success, string message)
  2.     {
  3.         Debug.Log(string.Format("network_onJoinChannel: channelID={0} success={1} message={2}", channelID, success, message));
  4.  
  5.         Vector3 pos = new Vector3(0, 0, 0);
  6.         Quaternion rot = Quaternion.Euler(new Vector3(0, 0, 0));
  7.         string playername = TNManager.GetPlayerData<string>("CharacterName") + tno.ownerID;
  8.  
  9.         if (manager_instance.isinsetup)
  10.             TNManager.Instantiate(1, 99, playerprefabPath, false, pos, rot, playername);
  11.  
  12.         if (manager_instance.isinlobby)
  13.             TNManager.Instantiate(2, 99, playerprefabPath, false, pos, rot, playername);
  14.  
  15.         if (manager_instance.isinworld)
  16.             TNManager.Instantiate(3, 99, playerprefabPath, false, pos, rot, playername);
  17.     }
  18.  
  19.  
  20.  
  21.  
  22.     // Define our remote creation call here with an id of 99
  23.     [RCC(99)]
  24.     static GameObject CreatePlayer(GameObject prefab, Vector3 pos, Quaternion rot, string pn)
  25.     {
  26.         // Instantiate the prefab
  27.         GameObject go = prefab.Instantiate();
  28.  
  29.         // Set the position and rotation based on the passed values
  30.         Transform t = go.transform;
  31.         t.position = pos;
  32.         t.rotation = rot;
  33.         go.name = pn;
  34.  
  35.         return go;
  36.     }

Tell me if that renders a different result.

acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 8
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: Trying to create a new object but get "Failed to call function"
« Reply #6 on: April 18, 2017, 06:21:04 AM »
(I recall there being one in an example for TNet) and if that is the case, I wonder if TNManager.Instantiate is picking the wrong method to call. Try calling the method by a defined rccid instead of by the func name.

Youre right! I changed the name of the function from "CreatePlayer" to "InstantiatePlayer" and it works without errors! Thanks for the help!




mythikos

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 15
    • View Profile
Re: Trying to create a new object but get "Failed to call function"
« Reply #7 on: April 18, 2017, 10:07:31 AM »
(I recall there being one in an example for TNet) and if that is the case, I wonder if TNManager.Instantiate is picking the wrong method to call. Try calling the method by a defined rccid instead of by the func name.

Youre right! I changed the name of the function from "CreatePlayer" to "InstantiatePlayer" and it works without errors! Thanks for the help!

No problem! Glad you got it working!