- Home /
Network.Instantiate c# issues
I rewrote my network manager script from Javascript to C#, and I ran into a problem. After a lot of debugging it seems the scripts still function same, except that the lines Instantiating my players, cameras, and game controller game objects no longer do anything. Seemed like an easy fix to me, but after a bunch of research on the problem I'm still pretty lost.
I've looked here, here, here, and here; I think some of these cases are the same problem I'm having, but I just don't understand the solution.
Here's one of my instantiate lines:
GameObject player = Network.Instantiate(playerPrefab,spawn.position,Quaternion.identity,0) as GameObject;
Is there something I'm missing as part of the c# syntax? Again, the same script in Javascript functions as expected without any problems with the instantiate.
Another very strange point is that in fact the c# script DOES instantiate my game objects when I run the game in the editor, but not when I build it. The build fails regardless of whether it is the client or server.
Edit:: continuing to baffle me. I've now set up the c# network manager to pass prefab variables to a .js script and the which instantiates the prefabs and then returns the instantiated objects as GameObjects back to the c# network manager script, but my variables are coming in pretty inconsistently. Still, if I do try the EXACT thing all in the c# method -- which is obviously much more ideal -- the prefabs don't get instantiated at all. Do c# scripts read prefabs differently???
In your last link, the playerPrefab was a Transform, when it should be a GameObject. Are you trying to NetworkInstantiate a Transform?
Nope, its a serialized GameObject variable declared outside of the method at the beginning of the class function :(
Although I do think that it's an issue with the way the script is reading my prefab, and not my translation of the original javascript.
Answer by andyspeak · Aug 25, 2013 at 06:31 PM
hey i had the same problem just now and found a way to get past it what you do is use
//this is a part of my code //leo is the name of my prefab
GameObject player = Network.Instantiate(Resources.Load("Leo"),SpawnLocation.position,SpawnLocation.rotation,0) as GameObject;
you need to make a folder in your assets and call it "Resources" and put your prefab there it worked for my android project so good luck
it alsow depends on how your "spawnlocation.position/rotation" is setup
u need to do this if u dont it prob still not work
public Transform SpawnLocation;
// Use this for initialization
void Start () {
GameObject go = GameObject.Find("Spawn");
SpawnLocation = go.transform;
}
p.s this is my 1st unity answer xp
Your answer
Follow this Question
Related Questions
Network.instantiate only works for Server to client 0 Answers
Player 2 not spawning 1 Answer
Instantiate a random item at a random position 1 Answer
Distribute terrain in zones 3 Answers