简体   繁体   中英

Unity3D Client cannot spawn prefab

I am making shooting game over network and it works very well with below source.

player.cs

void Update(){
    if (!isLocalPlayer) {
        return;
    }
    if (Input.GetMouseButton(0)) {
        CmdDefaultAttack(_skillDefault);
    }

}

[Command]
protected void CmdDefaultAttack(GameObject _attackObject) {

    GameObject bullet = (GameObject)Instantiate(_attackObject,_skill_Default_Spawn.position, _skill_Default_Spawn.rotation);

    bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 6.0f;

    ClientScene.RegisterPrefab(bullet);

    NetworkServer.Spawn(bullet);

    Destroy(bullet, 2);
}


but what i want to do is to implement CmdDefaultAttack in another class inherited Interface and call it from Player.cs

source below

public interface ISkill {
    void initiate(Player _player);
    void CmdAttack();
    bool CmdMotion();
}

public class SphereSkillDefault : NetworkBehaviour, ISkill {
    Plyaer player;
    public GameObject _skill_Default_Level1;
    public GameObject _skill_Default_Cannon

    public void initiate(Player_player) {
        this.player = _player;
    }
    [Command]
    public void CmdAttack() {
        GameObject bullet = (GameObject)Instantiate(_skill_Default_Level1, player._skill_Default_Spawn.position, player._skill_Default_Spawn.rotation);
        bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 6.0f;
        NetworkServer.Spawn(bullet);
        Destroy(bullet, 2);
    }

}

player.cs

    ISkill _Idefaultattack;
    public GameObject _defaultattack;

void Start() {
        if (NetworkServer.active)
            Debug.Log("Actived");
        else
            Debug.Log("DeActived");

        if (isLocalPlayer) {
            _Idefaultattack = _defaultattack.GetComponent<SphereSkillDefault>();
            _Idefaultattack.initiate(this);
        }
    }
    void Update() {
        if (!isLocalPlayer) {
            return;
        }
        _Idefaultattack.CmdAttack();
    }

The problem is when i try to shoot a bullet, it throws the error message saying "network server is not active. cannot spawn objects without an active server."

And only server player can shoot bullets and it actually shows up to clients. But clients can shoot only on their side and it doesn't show up to other clients or server.

i am stuck in this problem for days. Can anyone give me any advice?

thanks.

This problem is direct call to other class. so, you need bridge function. I have written the source.

ISkill _Idefaultattack;
public GameObject _defaultattack;

void Start() {
    if (NetworkServer.active)
        Debug.Log("Actived");
    else
        Debug.Log("DeActived");

    if (isLocalPlayer) {
        _Idefaultattack = _defaultattack.GetComponent<SphereSkillDefault>();
        _Idefaultattack.initiate(this);
    }
}
void Update() {
    if (!isLocalPlayer) {
        return;
    }
    //_Idefaultattack.CmdAttack();
    CmdAck();
}

[Command]
void CmdAck(){
    _Idefaultattack.CmdAttack();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM