Search Unity

Can't declare NetworkVariable

Discussion in 'Netcode for GameObjects' started by JSaunders, Oct 19, 2022.

  1. JSaunders

    JSaunders

    Joined:
    Aug 6, 2018
    Posts:
    17
    Hey all! I'm trying to declare a network variable for the current number of players, but whenever I declare the variable as an int I get the following: Any ideas?

    upload_2022-10-19_16-2-24.png
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,847
    That is odd. Which Netcode and Unity version?
    Does it work with bool and float?
     
  3. JSaunders

    JSaunders

    Joined:
    Aug 6, 2018
    Posts:
    17
    Unity 2021.3.2f1, Netcode for GameObjects 1.0.2

    It doesn't work for any value type I've used.
     
  4. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229
    private NetworkVariable<int> value = new NetworkVariable<int>(0);

    Thils line works for me. Are you using Unity.Netcode;
    using System.Collections;
    using System.Collections.Generic;?
    And did you connect visual studio to unity?
     
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,847
    Minimal working version. Does this work if you add it to your project in a new script file named NetVar.cs?
    Code (CSharp):
    1. using Unity.Netcode;
    2. public class NetVar : NetworkBehaviour
    3. {
    4.     private NetworkVariable<int> variable = new(1);
    5. }
    Does it work in the editor, or is only the IDE complaining?
     
  6. JSaunders

    JSaunders

    Joined:
    Aug 6, 2018
    Posts:
    17
    Yes across the board
     
  7. JSaunders

    JSaunders

    Joined:
    Aug 6, 2018
    Posts:
    17
    No, this new file also doesn't work, and it throws the same error whenver I try to run.
    It's also saying '' is a type not supported by the language, whihc means nothing to me
     
  8. JSaunders

    JSaunders

    Joined:
    Aug 6, 2018
    Posts:
    17
  9. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,847
    Could you create a new project, add Netcode to it, and see if that works?
    Do you get the same error in the Unity Editor Console window?
     
  10. JSaunders

    JSaunders

    Joined:
    Aug 6, 2018
    Posts:
    17
    The situation appears to have resolved itself with the 1.1.0 update this morning. Thanks all!