Trying To Spawn a Modified Prefab with NetworkServer.Spawn doesn't work.
Hello fine folks. You see, I've been trying to get along with multiplayer to attempt new features and online play. As such, i'm still very newb in these aspects.
Getting straight to the point: I have an extremely simple Asteroids esque space shooter, where the goal is for the players shoot each other to death. I got nearly everything synced properly but one critical thing doesn't work.
The bullets that are spawned shoot the player that spawned them.
This is because bullets use an "OwnerID" to identify which player owns the bullets so he doesn't get shot by his own bullets. Bullets that have custom "OwnerIDs" have been Initialized by the player with that ID thingy and also the same color as the player. So white bullets effectively have no Owner because they haven't been initialized. (They have white as default color and default Owner ID)
This is what happens when I attempt NetworkServer.Spawn(), it basically spawns the default bullet, causing the players to kill themselves, but on the host side, everything's fine and working normally.
How do I fix this?
void PewPewPew() // This is a bridge for the lack of a better term...
{
CmdInstantiateNetworkBullet();
}
[Command]
public void CmdInstantiateNetworkBullet()
{
BI = (GameObject)Instantiate(Bullet, transform.position, Quaternion.Euler(0, 0, transform.eulerAngles.z - 90));
BI.GetComponent<BulletScript>().InitializeVars(OwnerID, PColor);
NetworkServer.Spawn(BI);
}
To add, I tried to follow the Multiplayer FPS tutorial as closely as possible, their method works on my side, but I have to change the bullets to make them color coded and to prevent self shooting .Maybe I missed a step or something.
Well thanks for Reading! :)
I'm in the same situation, after the update to 5.6, the projectile that worked and showed up on the client stop working and giving error message "OnObjSpawn netId: 45 has invalid asset Id" while when hosting it don't. Please help, really need network for my project.
I ended up getting help on a Reddit thread, and a person suggested for me to use ClientRPC to fix the code. So it generates bullets on both the Clients and the Servers, it probably isn't the cleanest approach but it works great for now. :)
This worked for me, not sure about your problem tho.
[Command]
public void CmdInstantiateNetworkBullet()
{
BI = (GameObject)Instantiate(Bullet, transform.position, Quaternion.Euler(0, 0, transform.eulerAngles.z - 90));
BI.GetComponent<BulletScript>().InitializeVars(OwnerID, PColor);
transform.localScale *= 0.8f;
// NetworkServer.Spawn(BI);
RpcInsantiateClientBullets();
}
[ClientRpc]
public void RpcInsantiateClientBullets()
{
BI = (GameObject)Instantiate(Bullet, transform.position, Quaternion.Euler(0, 0, transform.eulerAngles.z - 90));
BI.GetComponent<BulletScript>().InitializeVars(OwnerID, PColor);
transform.localScale *= 0.8f;
}
Guess i can give it a try, so do i need to add the "Network Identity" component to my projectile gameobject?
An RTS is a tad ambitious compared to a simple arena shooter, don't you think?
The [ClientRpc] simply runes that code on the clients, while [Command] runs it on the server. I'm really new to this stuff so I'm sorry I can't help you as much as I would have liked.
Also the InitializeVars() is a method that Is run on the bullets and gives them their look n stuff. I have to run it twice on both [Command] and [ClientRPC] so it creates the bullets "correctly" on all clients and the server, still this doesn't stop some $$anonymous$$or desync issues, since they are created with delay and sometimes can miss in some clients and hit in others. $$anonymous$$y future solution to this is have the Server "sync" the proper variables periodically.
Also I have no idea what Network.Identity does, I just put it in there to prevent issues. $$anonymous$$aybe I don't need it. Who knows.
Best of luck in your game.
Also, I don't use a "weapon" everything is in the same player script. Except for the bullets, but their code is just angle based motion, the actual creation part was what originated this question.