- Home /
Network.Instantiate position in the corner
Im trying to create a new gameObject on the network.
Network.Instantiate(tree, tree.transform.position, tree.transform.rotation, 0)
Ive got then an error:
"The object tree must be a prefab in the project view."
It is silly , because Ive got tree as a prefab. When I change the code to:
GameObject tree2= Resources.Load("Models/tree", typeof(GameObject)) as GameObject;
tree2.transform.position = tree.transform.position;
tree2 = Network.Instantiate(tree2, tree.transform.position, tree.transform.rotation, 0) as GameObject;
It creates gameObjcet tree(Clone) in the corner of the terrain. I dont know why It is in this position or rotation. When I check it by:
print ( tree2.transform.position);
print ( tree2.name);
print ( tree.transform.position);
It shows proper positions , and the name is without "(Clone)", because I used before:
string tmpName = tree.name;
tree= Instantiate(tree,hit.point,Quaternion.identity) as GameObject;
tree.name = tmpName;
So the "Clone" should be deleted. It looks like the server is creating, from a prefab like it should, but why it doesnt put it in proper place and rotation?
Probably I can change it by RPC, but I set already coordinates in Network.Instantiate.
Ive changed the GameObject tree2 to Transform tree2 , but it is still the same. I dont understand why I cannot change the position or rotation, even if its changed in prefab. I can change name of the gameObject tree2.name and it works.
I discovered That
tree2.transform.position = Vector3.Lerp(transform.position, bVec, 1);
Doesnt work after Network.Instaniate(). It even doesnt work after when Update() goes from the begining. It works, hmm in the third "loop" of the Update().
Is it connected with the yeld? In my opinion the answer is that, tranform.position is forgotten, before the GameObject spawns on the network. But it doesnt answer why I should use such strange technics, why Network.Instantiate doesnt set the position and rotation at the begining of creation of the object.
Answer by Bunny83 · Jun 15, 2012 at 11:49 AM
You can only use prefabs with Network.Instantiate since Unity has to tell the other peers what they have to instantiate. When you use
tree = Instantiate(tree,hit.point,Quaternion.identity) as GameObject;
You overwrite the prefab reference with an instance which only exists on your pc. This object can't be instantiated in the network.
Is there a reason why you don't use
Network.Instantiate(tree,hit.point,Quaternion.identity);
?
It seems you have trouble to get into basic networking. There are not really network objects. Every player has it's own version of the object. The only link between them is a NetworkView with a ViewID. If you use Network.Instantiate, it takes care of assigning a new NetworkViewID to each NetworkView that is attacht to the object. If there's no NetworkView attached to your tree prefab, the prefab is instantiated on each client, but they have no relation at all. Each player has it's own tree.
The idea is that I first spawn tree on my loacl map with instanitate , and it follows the cursor. When i click I destroy the object , and in this position Im puting netwrok.instatiate().
Doing this before player plants the tree, only he can see it. After he plants , everyone has got the tree on their maps. Thats why Im using the reference, and I udnerstand the networking.
As I can see it, it was the prefab bug in blender. Because it works with older versions. I will try also the metod bunny83 proposed.
Answer by menep · Jun 15, 2012 at 11:38 AM
I've got the same bug as in this topic:
It is a bug in Unity, Blender. Object is spawned at 0,0,0 and it's hard to move it. I'm using version of Unity 3.5.1f2 and Blender 2.63. I will try to test it on Unity 3.5.2f2.
It has nothing to do with this behaviour. This is also not a bug. You have, like many others before, animated the root bone of your model which will hold it at 0,0,0 since that's the position you've modeled it. You have to either not animate the root bone at all (no animation curves / keyframes for the root bone), or parent your model to an empty gameobject and use this one.
I updated unity to 3.5.2f2 and it didnt help. So far I tested Blender: 2.63 -bug; 2.58a - bug; 2.44 - works; 2.47 - works; 2.55 - position works, but I dont see FBX model;
All 32 bits Windows.
Your answer
Follow this Question
Related Questions
Deleteing Objects created by Clients 0 Answers
Network Instantiate error even when connected? 1 Answer
Problems with Network.destroy 1 Answer