- Home /
[SOLVED] 2D Multiplayer: ERROR: Ready with no player object AND NullReferenceException: a null value was found...
Hi! I'm building a 2D Tank game with Unity for the mobile platform.
Here are the errors and warnings I receive when trying to shoot a bullet with my client (not host). These errors are showing up on the host/servers console: https://gyazo.com/8943960658e7db17aad2e10d64f109f7 [Elliots-iPhone] is my iphone (that I'm running the app on). It is the host of the game. Here's my code for shooting a bullet:
public void ShootBullet() {
if (isLocalPlayer) {
Vector3 pos_offset = new Vector3(0, 0, -3f);
Vector3 bullet_pos = bulletSpawn.transform.position - pos_offset;
GameObject bullet_fired = (GameObject)Instantiate(bullet, bullet_pos, this.transform.rotation);
CmdShootBullet_Server(bullet_fired);
Destroy(bullet_fired, 2.0f);
}
}
[Command]
private void CmdShootBullet_Server(GameObject bullet_fired) {
if (isServer) {
print("Shoot Bullet called on Server");
NetworkServer.Spawn(bullet_fired);
}
}
I'm not sure if they all are related but I think they are. Here's the detailed information on the red error: https://gyazo.com/e70762d8412c2a0a58ebfbf4e8f29f24
Answer by UDN_9a915d40-27e1-405b-b1cc-83be8be3e71d · Dec 31, 2017 at 06:04 PM
I solved it. The variable bullet_fired that was passed through as an argument to CmdShootBullet_Server was apparently null. I'm not sure why but i solved it by passing through a Vector3 pos and a Quaternion rotation. I then proceeded to manually creating my bullet instead of using a prefab since that gave some errors. Here's my new code:
public void ShootBullet() {
if (isLocalPlayer) {
Vector3 pos_offset = new Vector3(0, 0, -3f);
Vector3 bullet_pos = bulletSpawn.transform.position - pos_offset;
GameObject bullet_fired = (GameObject)Instantiate(bullet, bullet_pos, this.transform.rotation);
Destroy(bullet_fired, 2.0f);
CmdShootBullet_Server(bullet_pos, this.transform.rotation);
}
}
[Command]
private void CmdShootBullet_Server(Vector3 pos, Quaternion rot) {
if (isServer) {
print("Shoot Bullet called on Server");
GameObject shoot_bullet = CreateBullet();
shoot_bullet.transform.position = pos;
shoot_bullet.transform.rotation = rot;
print("POS OF BULLET ABOUT TO FIRE: " + shoot_bullet.transform.position);
// Spawn on clients
NetworkServer.Spawn(shoot_bullet);
}
}
private GameObject CreateBullet() {
GameObject b = new GameObject();
SpriteRenderer renderer = b.AddComponent<SpriteRenderer>();
renderer.sprite = Resources.Load("Sprites/Bullet", typeof(Sprite)) as Sprite;
/*NetworkIdentity network_identity = */b.AddComponent<NetworkIdentity>();
NetworkTransform network_transform = b.AddComponent<NetworkTransform>();
Rigidbody2D rigidbody2d = b.AddComponent<Rigidbody2D>();
CircleCollider2D circleCollider2D = b.AddComponent<CircleCollider2D>();
b.AddComponent<Bullet>();
rigidbody2d.gravityScale = 0;
network_transform.sendInterval = 0;
b.tag = "Bullet";
b.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
return b;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612145442im_/https://answers.unity.com/themes/thub/images/avi.jpg)