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

SetInteger not Working for Network Animator

Discussion in 'Netcode for GameObjects' started by WeeborWeed, Jan 24, 2022.

  1. WeeborWeed

    WeeborWeed

    Joined:
    Aug 31, 2020
    Posts:
    6
    So I was trying to synchronize the animator of my player across the network, however when I am trying to set any integer parameters that is not zero, the value of that field simply fluctuates randomly. And the code below is one of such example. When I set a non-zero integer weapon type, it goes all the way from MAX_INT to MIN_INT and then return to MAX_INT and start to go down again. And this piece of code is the only place where I am changing the param of Current Weapon Type, so can anyone tell me what was going wrong?
    Code (CSharp):
    1. public void SwitchWeapon()
    2.     {
    3.         animator.SetBool("reload", false);
    4.         equipTimer = equipCooldown;
    5.         DisableLineRender();
    6.         switch (weaponType)
    7.         {
    8.             case WeaponType.ASSULT_RIFLE:
    9.                 animator.SetInteger("current weapon type", 0);
    10.                 break;
    11.             case WeaponType.HAND_GUN:
    12.                 animator.SetInteger("current weapon type", 1);
    13.                 break;
    14.             case WeaponType.GRENADE:
    15.                 animator.SetInteger("current weapon type", 2);
    16.                 break;
    17.             default:
    18.                 Debug.Log("this weapon type is not yet implemented");
    19.                 break;
    20.         }
    21.     }
     
    okiaki likes this.
  2. luke-unity

    luke-unity

    Unity Technologies

    Joined:
    Sep 30, 2020
    Posts:
    306
    Is this code running on the server or the client? You have to set animator parameters on the server only.
     
  3. WeeborWeed

    WeeborWeed

    Joined:
    Aug 31, 2020
    Posts:
    6
    This happens to the host as well so I guess it really doesn't matter who's in control, all animators have this fluctuating integer parameter problem.
     
  4. MarceloAgostinho

    MarceloAgostinho

    Joined:
    Jun 6, 2017
    Posts:
    2
    Hello Luke!
    Would you mind to provide a piece of code about how to properly execute this call? Is it a simple "ServerRPC" or is there any extra config/code that is needed to work?

    Thank you!
     
  5. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    Code (CSharp):
    1. public void SwitchWeapon()
    2.     {
    3.       if(IsServer)
    4.       {
    5.          //your stuff here.
    6.        }
    7.        Debug.Assert(IsServer, "This is not the server");
    8.      
    9. }
    If you add that to your function what happens? If it asserts look at what calls that function and ensure its only called from the server.
     
  6. MarceloAgostinho

    MarceloAgostinho

    Joined:
    Jun 6, 2017
    Posts:
    2
    Hey @hoesterey! Sorry for my late reply, took a few days off :)
    Thank you for your answer! I've added the IsServer check and it didn't work until I realize that I was setting the wrong Animator Parameter! :D

    Thank you for taking your time to reply to my post anyway! ;)
     
    hoesterey likes this.