- Home /
Not sure why I keep getting a Null Reference Exception? (Networking)
Hi there, I'm kind of new to the networking side of Unity and I'm using the basic Unity networking library.
I'm aiming to make my enemies fire a bullet because I am trying to make a Space invaders style game, but an issue I have is that I keep getting a NullReferenceException in one of my scripts.
My bullet is spawned but then when the bullet gets spawned the error appears and I can't test the game further. Here is the error I get:
NullReferenceException: Object reference not set to an instance of an object Game.Update () (at Assets/Scripts/Game.cs:57)
And here is my code snippets for the 2 scripts in question.
Game.cs Update function
public class Game : MonoBehaviour { public bool running;
public List<GameObject> bullets;
public List<GameObject> newBullets;
public GameObject sphereEnemy, rotatorEnemy;
private static int enemyCount;
private Rect bulletCountPos, resetGamePos, startGamePos, gameBoxPos, gameRunningPos;
// Use this for initialization
void Start ()
{
resetGamePos = new Rect(10, 70, 150, 20);
startGamePos = new Rect(10, 90, 150, 20);
bullets = new List<GameObject>();
newBullets = new List<GameObject>();
bulletCountPos = new Rect(Screen.width - 200, 10, 200, 20);
gameBoxPos = new Rect(Screen.width - 210, 10, 200, 130);
gameRunningPos = new Rect(Screen.width - 200, 30, 200, 20);
}
// Update is called once per frame
void Update ()
{
if (running)
{
if (Network.peerType == NetworkPeerType.Server)
{
//Debug.Log("Updating bullet list");
Bullet bulletScript;
EnemyBullet enemyBulletScript;
newBullets = new List<GameObject>(bullets);
bullets.Clear();
// check all local bullet flight times
// Network.Destroy any over time
foreach(GameObject bullet in newBullets)
{
// check if bullet still exists (because the collision trigger messages can run during this update loop)
if (bullet == null) continue;
bulletScript = (Bullet)bullet.GetComponent("Bullet");
enemyBulletScript = (EnemyBullet)bullet.GetComponent("EnemyBullet");
if (bulletScript.currentFlightTime < bulletScript.maxFlightTime)
{
bullets.Add(bullet);
}
else
{
// tell all clients to destroy the bullet
Debug.Log("Ordering clients to remove " + bullet.name);
networkView.RPC("DestroyBullet", RPCMode.Others, bullet.name);
// destroy local bullet on server
Destroy(bullet);
}
}
// clear the newBullets list
newBullets.Clear();
//Debug.Log("Finished updating bullet list");
}
}
}
SmartEnemyController
// are we the server or a client?
if (Network.isServer)
{
Debug.Log("Server is spawning EnemyBullet" + enemyBulletCount + " fired by " + gameObject.name);
// call FireBullet RPC in clients
networkView.RPC("FireBullet", RPCMode.Others, "EnemyBullet"+enemyBulletCount);
// create bullet on local server
GameObject bullet = (GameObject)Instantiate(enemyBulletPrefab, transform.position, enemyBulletPrefab.transform.rotation);
bullet.name = "EnemyBullet"+enemyBulletCount;
((EnemyBullet)bullet.GetComponent("EnemyBullet")).setFiredBy(gameObject.name);
// add bullet to server list of bullets
game.addBullet(bullet);
enemyBulletCount++;
}
else
{
networkView.RPC("Log", RPCMode.Server, "EnemyBullet fired"+enemyBulletCount);
// call server FireBullet RPC
networkView.RPC("ServerFireBullet", RPCMode.Server, gameObject.name);
}
Thanks in advance for the help guys.