Network.Instantiate problems

I haven’t read the whole forum yet, but I need an answer to this quickly… I’m hoping someone knows this. I have a local only networked game, and have the server and client working.

It’s a flight simulator, and when I instantiate an airplane, I need to have it show up on all the clients and work.

I got this partially working after following a tutorial, but there are problems. Network.Instantiate doesn’t seem to work if I use a GameObject in the scene. It has to be a prefab. I made the GameObjects (planes) prefabs, but some of the parts of the GameObject don’t show up in the prefab… don’t know why… This is problem #1.

So, I can Network.Instantiate a prefab, but if I do this from the computer that started the server, I have problems. I think it may be because I need to disable the server from responding to an Network.Instantiate. I set up an event function for OnNetworkInstantiate, but the server starting computer doesn’t seem to get this event… I did this:

void OnNetworkInstantiate(NetworkMessageInfo info)
{
Debug.Log(networkView.viewID + " spawned");
if (Network.isServer)
{
Network.RemoveRPCs(networkView.viewID);
Network.Destroy(gameObject);
}
}

So, I was thinking I would use my local Instantiate to setup a plane, then Network.Instantiate to make it on the clients… but that is bad because the NetworkView has an “Observed” transform… you plug in the prefab… but it won’t take a GameObject in the scene.

So I guess I have to use Network.Instantiate to spawn every object including the local one, but I have camera problems… This is problem #2

All the cameras on the planes are part of scripts on the planes, and I have a system them switches cameras according to the plane… The cameras DON’T FOLLOW WITH THE GAMEOBJECT WHEN I MAKE IT A PREFAB!! I don’t know why. The prefab doesn’t seem to copy more than one level down in the GameObject.

Does anyone know how I should do this? I need to control the cameras for each plane, and yet instantiate them on the network and locally. I only want to control the local plane, and have the network planes track… I thought this would be almost automatic… because it’s such a common thing, but NO… Does anyone know a solution?

Thanks

If it’s already in the scene, why would you wan’t to add it again ?

You need to resolve this issue, maybe that’s why instantiating doesn’t work correctly.

No, you don’t need to do that.

It should get the event.
I think you need to read up on how to instantiate, the scripting reference has all the examples needed to get it working.
Maybe your tutorial isn’t the best one, or you should try and make a much more simpler game for now.
Network.Instantiate should do everything you need out of the box.
I suggest you create a blank project and create a very simple networked game, just using boxes and get that working and understand what your actualy doing. It’s not as simple as it looks.

Thanks appels for your reply…

I want your advice about the best way to approach the network design for my flight simulator…

See, the GameObjects I instantiate in the non-networked version, are multiple layers deep. I have folders for Physics, Visual Mesh, Cameras, Wheels, etc… and scripts in each folder… I need access to all the parts in the Instantiating computer… to fly it.

You said I had to solve the first problem of prefabs… here’s my problem though…
When I make a prefab out of these planes, I only get ONE layer deep on the prefab. Of course the folder icon changes to a cube, and only 1 layer of cubes under the first cube. THERE IS ONLY ONE WAY TO MAKE A PREFAB, that I know of… I go to the Project TAB, select which folder in the Project Tab I want to put the prefab in, and select Create->prefab… making a blank prefab… I rename it, and drag the gameobject on top of it.

What else should I do to solve the single layer problem?

NOW about Instantiating.

Assuming the server does all the Instantiating,
I found an RPC way to “CustomInstantiate” Using “RPCmode.OthersBuffered” only clients will instantiate. Like this:

networkView.RPC(“CustomInstantiate”, RPCMode.OthersBuffered, Network.AllocateViewID());

then this does the instantiating on the client:

[RPC]
void CustomInstantiate(NetworkViewID manuallyAllocatedViewID)
{
//instantiate the prefab and store a reference to it so we can change it’s viewID
NetworkAwarePrefab1.SetActive(true);
var o = Instantiate(NetworkAwarePrefab1, transform.position, Quaternion.identity) as Transform;

//change the viewID of the instantiated object to the one we allocated when the method was called
o.networkView.viewID = manuallyAllocatedViewID;
}

I make the objects they instantiate network aware by attaching networkView and I added a script to move the planes using OnSerializedNetworkView() like this:

void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
if (stream.isWriting)
{
Vector3 pos = transform.position;
stream.Serialize(ref pos);
}else{
Vector3 receivedPosition = Vector3.zero;
stream.Serialize(ref receivedPosition);
transform.position = receivedPosition;
}
}

Is this the best way to do this… considering I can’t make prefabs Instantiate correctly on the Server, because folders and cameras are missing…

I tried using the simpler:

Network.Instantiate(NetAwarePrefab, trans.position, trans.rotation, 0);

but I get double planes Instantiated… on the server, and my multiple camera system doesn’t work because the Camera Object I use in the Plane Camera script CAN’T BE ATTACHED, because I don’t have access to that script variable, or the script, on the prefab…

Do you see my dilemma?

What in your opinion is the best way to approach this?

Thanks…

OK, I found a solution to the PREFAB problem.

In the Start event of the Camera script for the airplane, I used GameObject.Find to put the camera into the camera variable I need.

It took me a little while to think of this, I’m still not sure why the Prefab design doesn’t allow access to the full object in the editor…

So, I am going back to trying the Network.Instantiate solution to my system since right now I really only need the transform to track across the network.

I may end up including RPCs too.

Any thoughts about RPC vs automatic…with networkView?

Thanks in advance…

I’d like a little more advice about Unity Network design…

I found that using an [RPC] to instantiate like this:

myTransform = Instantiate(NetworkAwarePrefab1, transform.position, Quaternion.identity) as Transform;

has a timing issue when using the debugger…I think…

sometimes myTransform is null, but not always. The NetworkAwarePrefab1 is valid.

Is this typical behaviour? an RPC in the debugger?

I took your suggestion appels, and made another project just following the network tutorial, which is very good at palladium games here:

palladiumgames.net/tutorials/unity-networking-tutorial/

I worked out all my network problems and more. I now have a system that tracks players who login, and allows the server to spawn an object on any player that connects… this is the functionality I needed.

At first, I read through the tutorial and tried to implemented it directly because I thought I understood it. There are several subtleties about making objects and how networkView works that I didn’t get by reading. Sometimes hands on following a tutorial exactly step by step is the best method…

However! I have complaints about Unity and monodev. First in the middle of trying to make this work my debugger decided to NOT connect. The breakpoints went blank and I lost the yellow line tracker… I tried several things, and fortunately I have 2 identical computers that I develop on. I upgraded to Unity 4.3.1 pro and monodev 4.0.1. That didn’t work on my main dev computer but DID WORK for A WHILE on my 2nd computer, however after a while it did the same thing…

I reloaded 4.2.2 and monodev 2.8.1 but it didn’t work… Fortunately I backed up all my project files because upgraded makes a non-reversable change to your project…

Unity really needs to develop their own Editor/Debugger. It should work entirely in the Unity IDE. Monodev has their own agenda, and that causes problems. I wasted time trying to work through it, and it made everything harder

Now, I also found out something about compiler errors that doesn’t make sense. I’m going to start another thread with an example that made an error, but the error wasn’t what the message said. This is another reason Monodev needs to be taken over… Look for RPC Network.Instantiate error.

Thanks