- Home /
[C#] Unet spawn dont work.
Hi! I have this code:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class PlayerShooting : NetworkBehaviour {
[SerializeField] GameObject BulletObj;
public Transform BulletSpawn;
void Start(){
BulletObj = (GameObject)Resources.Load("Prefabs/Bullet");
if (!isLocalPlayer) {
enabled = false;
}
}
void Update () {
if (Input.GetButtonDown ("Shoot")) {
CmdSpawnBullet(BulletSpawn.position);
}
}
[Command]
void CmdSpawnBullet(Vector3 pos){
GameObject Bullet = (GameObject)Instantiate (BulletObj, BulletSpawn.position/*new Vector3(transform.position.x,0.328f,transform.position.z)*/, this.gameObject.transform.rotation);
ClientScene.RegisterPrefab(Bullet);
NetworkServer.Spawn (Bullet);
}
}
1- This code should instantiate a bullet. This work in the server, but when you press the fire button in the client, the bullet dont spawn...
2- I have this error in the console when i spawn the bullet correctly in the server:
This have no sense, because i attach the "bulletspawn" and the bullet spawn on the server and client...
I am not sure about your error it could have something to do with using a Command, but don't quote me on that. I don't think you should have to spawn the bullet through a Command on the server. See how they do it here.
Ok the problem of instantiate is solved, but the error message persist... And this have no sense x´D
Without seeing inspector for PlayerShooting hard to tell. From you other post you did not have anything assigned so it would be your standard null ref exception.
What does the inspector look like on the object when you in play mode in the editor and get the error. See if the referenced object is getting unreferenced when you play.
also I would edit your original posts code to reflect the changes you made.
Your answer
Follow this Question
Related Questions
Change NetworkMatch's name, attributes, and password after creation 1 Answer
Unity 2017 Unet - Dedicated Server 1 Answer
UNET Network Discovery sends data with extra characters 0 Answers
ListMatchResponse Matches Count is 0 on remote PC?!? Why is Matchmaking broken? 0 Answers
Several Unity Multiplayer questions 0 Answers