Search Unity

Local Networking, getting a null error when spawning

Discussion in 'Multiplayer' started by Sha_ma, Oct 15, 2019.

  1. Sha_ma

    Sha_ma

    Joined:
    Jun 14, 2019
    Posts:
    8
    I have gameObjects (the enemies) spawned one by one by a spawner script.
    The spawner script have an arrey of Serialized scriptableObjects (waveConfig), each containing different variables regarding the gameObjects.

    The spawner looks like this:
    Code (CSharp):
    1.    
    2. private IEnumerator SpawnAllEnemies(WaveConfig waveConfig)
    3.     {
    4.  
    5.         for (int enemyCount = 0; enemyCount < waveConfig.GetNumberOfEnemy(); enemyCount++)
    6.         {
    7.                  newEnemy = Instantiate(
    8.                 waveConfig.GetEnemyPrefab(),
    9.                 waveConfig.GetWayPoints()[0].transform.position,
    10.                 Quaternion.identity) as GameObject;
    11.             newEnemy.GetComponent<EnemyPathing>().SetWaveConfig(waveConfig);
    12.            SpawnEnemy();
    13.             yield return new WaitForSeconds(waveConfig.GetTimeBetweenSpawns());
    14.         }
    15.     }
    16.         private void SpawnEnemy()
    17.     {
    18.         NetworkServer.Spawn(newEnemy);
    19.      }
    When I run on both a local build and the editor,
    with the build as server,
    the build runs just fine, and on the editor I can see the gameObjects spawned in the same location (but not the editors' player) and the screen is stuck with this error:
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. EnemyPathing.Start () (at Assets/Scripts/EnemyPathing.cs:14)
    If the build as the client and the editor the server, it seems to run OK, but the bulid is giving the error.

    any Ideas ?
     
    Last edited: Oct 15, 2019
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The error says it occurs in EnemyPathing.Start() on line 14. Please show that method and state which line is 14.
     
  3. Sha_ma

    Sha_ma

    Joined:
    Jun 14, 2019
    Posts:
    8
    Code (CSharp):
    1. public class EnemyPathing : MonoBehaviour
    2. {
    3.     WaveConfig waveconfig;
    4.     float moveSpeed;
    5.     List<Transform> waypoints;
    6.     int waypointIndex = 0;
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.     moveSpeed = waveconfig.GetMoveSpeed();   [I]<== line14[/I]
    11.     waypoints = waveconfig.GetWayPoints();
    12.     transform.position = waypoints[waypointIndex].transform.position;
    13.     }
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,070
    The waveconfig is not set to any value before you use it.
     
  5. Sha_ma

    Sha_ma

    Joined:
    Jun 14, 2019
    Posts:
    8
    Ok, so the spawner is calling:
    Code (CSharp):
    1.             newEnemy.GetComponent<EnemyPathing>().SetWaveConfig(waveConfig);
    This line (from the first post) is a part of a corutine called by another corutine called in the start method.

    It is calling a methods on the EnemyPathing script:
    Code (CSharp):
    1.     public void SetWaveConfig(WaveConfig waveconfig)
    2.     {
    3.         this.waveconfig = waveconfig;
    4.     }
    It seems that everything working fine until a client is connecting. I have also tried make the spawner script run only on the server with:

    Code (CSharp):
    1.   void Start()
    2.     {
    3.                 if (isServer == true)
    4.                 {
    5.                StartCoroutine(beginSpawningCoroutine());  
    6.                  }
    It did not help. dos it have to do with who is calling the script or how many times it is caled ?
     
    Last edited: Oct 16, 2019
  6. Sha_ma

    Sha_ma

    Joined:
    Jun 14, 2019
    Posts:
    8
    Thank you for engaging !
    I have found a solution, but still have a question..

    The thing was that EnemyPathing script (which is part of every enemy spawned),
    has a Start and Update methods that access the varuable (waveconfig).

    I have added:
    Code (CSharp):
    1.        if (hasAuthority == false)
    2.         {
    3.             return;
    4.         }
    Now, from what I understand, this would only let the server access to this script, right ?
    Is there another way to do so without having to chack for hasAuthority for every gameObject every frame ?
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If the server has authority over the networked gameobject then yes hasAuthority would only be true on the server. It is possible to assign authority to one of the clients though, and in that case hasAuthority would be true on that client. You can also try checking isServer.