@@ -332,9 +332,57 @@ public class PlayerState : NetworkBehaviour
332
332
// The weapon booster currently applied to a player
333
333
private NetworkVariable <WeaponBooster > PlayerWeaponBooster = new NetworkVariable <WeaponBooster >();
334
334
335
- // A list of team members active "area weapon boosters" that could be applied if the local player
336
- // is within their range.
337
- private NetworkList <AreaWeaponBooster > TeamAreaWeaponBoosters = new NetworkList <AreaWeaponBooster >();
335
+ /// <summary >
336
+ /// A list of team members active "area weapon boosters" that could be applied if the local player
337
+ /// is within their range.
338
+ /// </summary >
339
+ private NetworkList <AreaWeaponBooster > TeamAreaWeaponBoosters ;
340
+
341
+ void Awake ()
342
+ {
343
+ // NetworkList can't be initialized at declaration time like NetworkVariable. It must be initialized in Awake instead.
344
+ TeamAreaWeaponBoosters = new NetworkList <AreaWeaponBooster >();
345
+ }
346
+
347
+ void Start ()
348
+ {
349
+ /* At this point, the object has not been network spawned yet, so you're not allowed to edit network variables! */
350
+ // list.Add(new AreaWeaponBooster());
351
+ }
352
+
353
+ void Update ()
354
+ {
355
+ // This is just an example that shows how to add an element to the list after its initialization:
356
+ if (! IsServer ) { return ; } // remember: only the server can edit the list
357
+ if (Input .GetKeyUp (KeyCode .UpArrow ))
358
+ {
359
+ TeamAreaWeaponBoosters .Add (new AreaWeaponBooster ()));
360
+ }
361
+ }
362
+
363
+ public override void OnNetworkSpawn ()
364
+ {
365
+ base .OnNetworkSpawn ();
366
+ if (IsClient )
367
+ {
368
+ TeamAreaWeaponBoosters .OnListChanged += OnClientListChanged ;
369
+ }
370
+ if (IsServer )
371
+ {
372
+ TeamAreaWeaponBoosters .OnListChanged += OnServerListChanged ;
373
+ TeamAreaWeaponBoosters .Add (new AreaWeaponBooster ()); // if you want to initialize the list with some default values, this is a good time to do so.
374
+ }
375
+ }
376
+
377
+ void OnServerListChanged (NetworkListEvent <AreaWeaponBooster > changeEvent )
378
+ {
379
+ Debug .Log ($" [S] The list changed and now contains {TeamAreaWeaponBoosters .Count } elements" );
380
+ }
381
+
382
+ void OnClientListChanged (NetworkListEvent <AreaWeaponBooster > changeEvent )
383
+ {
384
+ Debug .Log ($" [C] The list changed and now contains {TeamAreaWeaponBoosters .Count } elements" );
385
+ }
338
386
}
339
387
340
388
/// <summary >
0 commit comments