Question by
Metalel · Mar 23, 2016 at 01:02 PM ·
networkinginstantiatenullargumentexception
The thing i want to instantiate is null?
Code is supposed to summon a projectile being shot form 1 player to all clients. While the host can summon the projectile for all clients, the clients can't summon, and get the error: ArgumentException: The thing you want to instantiate is null.
using UnityEngine;
using System;
using UnityEngine.Networking;
public class CharacterController : NetworkBehaviour
{
public float fireballSpeed = 6;
public GameObject fireballPrefab;
public Transform fireballSpawn;
void FixedUpdate()
{
if (!isLocalPlayer)
{
return;
}
Attack();
}
void Attack()
{
if (Input.GetButtonDown("Fire1"))
{
CmdFireProjectile(fireballPrefab, fireballSpeed, fireballSpawn.position,fireballSpawn.rotation);
}
}
[Command]
void CmdFireProjectile(GameObject projectilePreFab, float projectileSpeed, Vector3 projectileSpawnPosition, Quaternion projectileSpawnRotation)
{
GameObject projectile = (GameObject)Instantiate(projectilePreFab, projectileSpawnPosition, projectileSpawnRotation);
projectile.GetComponent<Rigidbody>().velocity = projectile.transform.forward * projectileSpeed;
NetworkServer.Spawn(projectile);
Destroy(projectile, 2);
}
}
}
Comment