[uNet] how to spawn the object

You can only use NetworkServer.Spawn if you’re the host. Other players should send a Command to the host to spawn stuff. I noticed your method isn’t decorated with the Command attribute - it has to be.

First method:

    public override void Fire()
    {
        Debug.Log(CurrentAmmoInto + "/" + CurrentMagazines);
        if (Input.GetButton("Fire") && CurrentAmmoInto > 0 && ShootTimer <= 0 && ReloadTimer <= 0)
        {
            FireParticle();

            var AnotherProjectile = ProjectileTypePrefab;
            Bullet proj = AnotherProjectile.GetComponent<Bullet>();
            proj.Damage = Damage;
            CmdSpawn(AnotherProjectile, FirePosition.transform.position, FirePosition.transform.rotation);

            Vector3 target = new Vector3(FirePosition.transform.position.x * Random.Range(-FireDispersionRadius, FireDispersionRadius), FirePosition.transform.position.y * Random.Range(-FireDispersionRadius, FireDispersionRadius), FirePosition.transform.position.z);
            target.z *= MaxDistance;

            AnotherProjectile.GetComponent<Rigidbody>().AddForce(Vector3.RotateTowards(transform.forward, target, Time.deltaTime, 0f) * FireForcePower);

            Debug.Log("Shot");
            ShootTimer = FireShootingSpeed;
            CurrentAmmoInto--;
            m_AudioSource.PlayOneShot(ShootClip);
            /*Vector3 targetDir = transform.position * 100 - transform.position;
            float step = 100 * Time.deltaTime;
            Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0F);
            Debug.DrawRay(transform.position, newDir, Color.red);
            transform.rotation = Quaternion.LookRotation(newDir);*/
        }
    }

Second Method:

    [Command] void CmdSpawn(GameObject Object, Vector3 position, Quaternion rotation)
    {
        Object = Instantiate(Object, position, rotation) as GameObject;
        NetworkServer.Spawn(Object);
    }

The above methods do not work, and more specifically - spawn.

Nothing there is striking me as wrong, the only thing that comes to mind is if you have set Local Player Authority in your NetworkIdentity in inspector.

AnotherProjectile isn’t the networked projectile so manipulating its Rigidbody won’t do what you think it will.

EDIT:

I bet your input code is running for every networked prefab. You should check isLocalPlayer so it only runs for the objects the player controls.

And third:

    public override void OnStartClient()
    {
        ClientScene.RegisterPrefab(ProjectileTypePrefab);
    }

No, there is no Local Authority

I would now at least the object to spawn.
the bullet has not LocalPlayerAuthority.
.

checked LocalPlayerAuthority on the projectile so that that’s not exactly working. and even locally does not appear.

EDIT:
checked the script input off on other prefab locally for everyone but himself.

Set LocalPlayerAuthority for the gameObject that has the script spawning bullets. Without LocalPlayerAuthority scripts can’t(from what I know) use Commands.


I put down, but something does not work.

I think you can only have NetworkIdentity on root gameObject, and Commands too, so remove the second NetowrkIdentity and find a way to have the Command in root gameObject.

1 Like

Therefore, for the sake of the test script hung the weapons on the main object. Worked. Probably have to do object Spawner with the script spawn and giving the link arms, to invoke the method.

so, now the error is more fun. He says, well, what object to spawn there. And so, the actual question is: how to spawn an object from a child object?

Soo you can have method in the root object like:

[Command]
public void CmdSpawnPrefab(GameObject prefab){
GameObject go = Instantiate(prefab) as GameObject;
NetworkServer.Spawn(go);
}

And call it from the child weapon with the right bullet as argument.

I use this method:

    [Command] public virtual void CmdSpawn(GameObject Object, Vector3 position, Quaternion rotation)
    {
        Object = Instantiate(Object, position, rotation) as GameObject;
        NetworkServer.Spawn(Object);
    }

But the console says when I call it, Nullreferenceexception. In this case, all of what is going on call it is not happening.

You can check when the object becomes null.

1 Like

Thank you, I did itю

EDIT:
And Yes, I also want to ask to apply physics to Rigidbody also need to do it via the [Command]?

started test with two clients. The client and host, creates all fine. And here’s one that a client can not do anything.

if one client works and other doesn’t, you will have to find some mistake somewhere, and the yes, speed to rigidbody can be also applied in the command.

For the answer to the question about physics thank you. And what about this problem, I think I missed something.

Frankly I have no idea what might do that, so you will have to check which methods are running, which aren’t, generally just find the problem.

Randomly do not register prefabs in networkmanager, where SpawnableNetworkPrefabs?

If you mean that the prefab sometimes doesn’t register, you can either figure out why does it happen, something must make the code not run, like the class that registers it isn’t active, isn’t in the scene etc, or you can just put the prefab in RegisteredSpawnablePrefabs in NetworkManager in the inspector, and then you don’t have to use NetworkServer.RegisterPrefab()

I think I found it. The clients somehow do not register prefabs.