我已经玩了很长时间了,现在我正在设置多人游戏。问题是我尝试将对象放在每个客户端上的任何方式都不起作用。抛出错误或对象不会出现在任何地方。我的脚本在JS中。这是:
import UnityEngine.Networking;
public class minvantory extends NetworkBehaviour {
var blockLayer : LayerMask = 1;
var range : float = Mathf.Infinity;
var hit : RaycastHit;
var PIC : Texture;
var invantory : Texture;
var itemnumber : int = 1;
var block1 : GameObject;
var block2 : GameObject;
var block3 : GameObject;
var block4 : GameObject;
var block5 : GameObject;
var block6 : GameObject;
var block7 : GameObject;
var block8 : GameObject;
var block9 : GameObject;
var block10 : GameObject;
var block11 : GameObject;
var block12 : GameObject;
var block13 : GameObject;
var block14 : GameObject;
var block15 : GameObject;
var block16 : GameObject;
var block17 : GameObject;
var block18 : GameObject;
var block19 : GameObject;
@SyncVar
var block : GameObject;
var open : boolean;
var escopen: boolean;
var id : int = 0;
var blocks : int = 0;
var player : GameObject;
var networked=false;
var customSkin : GUISkin;
var easter : GameObject;
var missile: GameObject;
//var x : playerlook;
function Start(){
if (!isLocalPlayer)
return;
blocks = PlayerPrefs.GetInt("blocks");
//x = GameObject.Find("MainCamera").GetComponent(playerLook);
Screen.lockCursor=true;
Cursor.visible=true;
}
function Update() {
if (!isLocalPlayer)
return;
if(Input.GetKeyUp(KeyCode.E)&&!escopen)
{
if (open)
open = false;
else
open = true;
Screen.lockCursor=!open;
Debug.Log("Open: " + open);
}
if(Input.GetKeyUp(KeyCode.Escape)&&!open)
{
if (escopen)
escopen = false;
else
escopen = true;
Screen.lockCursor=!escopen;
Debug.Log("Escopen: " + escopen);
}
if ((Input.GetMouseButtonDown(1))||(Input.GetKeyUp(KeyCode.X))&&!open&&!escopen&&!getCopn()){
Debug.Log("BUILD");
CmdBuild(block, id);
}
if (Input.GetMouseButtonDown(0)&&!open&&!escopen&&!getCopn()) CmdErase();
}
function OnGUI (){
if (!isLocalPlayer)
return;
GUI.skin = customSkin;
//GUI.DrawTexture(Rect(Screen.width-5,Screen.height-5,5,5), PIC, ScaleMode.ScaleToFit, true, 1);
if(open){
GUI.DrawTexture(Rect(0,50,350,500), invantory, ScaleMode.StretchToFill, true, 1);
var tblock : GameObject=block;
if (GUI.Button (Rect (10,80,120,20), "urainium")) {
id = 1;
tblock = block1;
}
if (GUI.Button (Rect (10,120,120,20), "nutrongun")) {
tblock = block2;
id = 2;
}
if (GUI.Button (Rect (10,160,120,20), "zaneite")) {
tblock = block3;
id = 3;
}
if (GUI.Button (Rect (10,200,120,20), "gravium")) {
tblock = block4;
id = 0;
}
if (GUI.Button (Rect (10,240,120,20), "asteroid")) {
tblock = block5;
id = 4;
}
if (GUI.Button (Rect (10,280,120,20), "gas")) {
tblock = block6;
id = 5;
}
if (GUI.Button (Rect (10,320,120,20), "steel")) {
tblock = block7;
id = 6;
}
if (GUI.Button (Rect (10,360,120,20), "rock")) {
tblock = block8;
id = 7;
}
if (GUI.Button (Rect (10,400,120,20), "dish")) {
tblock = block9;
id = 8;
}
if(GUI.Button(Rect(10,440,120,20), "rocket")){
tblock = block11;
id = 9;
}
if(GUI.Button(Rect(10,480,120,20), "crate")){
tblock = block10;
id = 10;
}
if(GUI.Button(Rect(140,80,120,20), "Y stabilizer")){
tblock = block14;
id = 13;
}if(GUI.Button(Rect(140,120,120,20), "planet core")){
tblock = block15;
id = 14;
}
if(GUI.Button(Rect(140,160,120,20), "Control")){
tblock = block16;
id = 15;
}
if (GUI.Button (Rect (140,200,120,20), "lamp")) {
tblock = block17;
id = 16;
}
if (GUI.Button (Rect (140,240,120,20), "glass")) {
tblock = block18;
id = 17;
}
if (GUI.Button (Rect (140,280,120,20), "plasma explosive")) {
tblock = block19;
id = 18;
}
if(GUI.Button(Rect(140,320,120,20), "computer")){
tblock = block12;
id = 11;
}
if(GUI.Button(Rect(140,360,120,20), "stabilizer")){
tblock = block13;
id = 12;
}
if(GUI.Button(Rect(140,400,120,20), "Easterment")){
tblock = easter;
id = 10000;
}if(GUI.Button(Rect(140,440,120,20), "ICBM Launcher")){
tblock = missile;
id = 2030;
}
CmdSyncBlock(tblock);
//}
}
else if(escopen){
GUI.DrawTexture(Rect(0,50,350,500), invantory, ScaleMode.StretchToFill, true, 1);
if (GUI.Button (Rect (10,60,280,40), "quit")) {
saveWorld();
Application.LoadLevel("GUI");
}
if (GUI.Button (Rect (10,110,280,40), "reset and quit")) {
PlayerPrefs.DeleteKey("saved");
Application.LoadLevel("GUI");
}
if (GUI.Button (Rect (10,160,280,40), "suicide")) {
GetComponent.<mplayer>().health=0;
}
}else
GUI.DrawTexture(Rect((Screen.width - PIC.width) / 2, (Screen.height - PIC.height) /2, PIC.width, PIC.height), PIC);
}
@Command
function CmdSyncBlock(g: GameObject){
block=g;
}
@Command
function CmdBuild(obj : GameObject, x : int){
var h: boolean=HitBlock();
Debug.Log(h);
if (h && hit.transform.tag != "Player"&&x!=2030) {
//Debug.Log(hit.transform.tag);
var blockobj : GameObject;
if(hit.transform.tag != "computer"&&hit.transform.tag!="thruster"){
Debug.Log("Spawning...");
blockobj = Instantiate(block, hit.transform.position+hit.normal, hit.transform.rotation);
NetworkServer.Spawn(blockobj);
blockobj.transform.rotation = hit.transform.rotation;
if(blockobj.GetComponent.<Rigidbody>() && hit.rigidbody){
blockobj.GetComponent.<Rigidbody>().velocity=hit.rigidbody.velocity;
blockobj.GetComponent.<Rigidbody>().angularVelocity=hit.rigidbody.angularVelocity;
}
blockobj.gameObject.AddComponent.<NetworkTransform>();
blockobj.gameObject.GetComponent.<NetworkTransform>().enabled=true;
Debug.Log("spawned.");
}else if(hit.transform.tag == "thruster"){
hit.transform.GetComponent("thrusterControl").Toggle();
}else if(hit.transform.tag == "computer"){
hit.transform.GetComponent("computer").Toggle();
Debug.Log("I <3 computers!");
}else{
Debug.Log("BUILD ERROR");
}
}
if(x==2030){
blockobj = Instantiate(missile, transform.position+transform.forward, transform.rotation);
NetworkServer.Spawn(blockobj);
blockobj.transform.rotation = transform.rotation;
blockobj.GetComponent.<ICBM>().follow=h;
if(blockobj.GetComponent.<Rigidbody>() && hit.rigidbody){
blockobj.GetComponent.<Rigidbody>().velocity=hit.rigidbody.velocity;
blockobj.GetComponent.<Rigidbody>().angularVelocity=hit.rigidbody.angularVelocity;
}
blockobj.gameObject.AddComponent.<NetworkTransform>();
blockobj.gameObject.GetComponent.<NetworkTransform>().enabled=true;
Debug.Log(h);
Debug.Log(hit);
if(!h)
return;
blockobj.GetComponent.<ICBM>().target=hit.transform.gameObject;
Debug.Log("FOLLOW!");
}
}
@Command
function CmdErase() {
if (HitBlock() && hit.transform.tag != "Player" && hit.transform.tag != "do not destroy"){
if(networked)
NetworkServer.Destroy(hit.transform.gameObject);
else
Destroy(hit.transform.gameObject);
}
//if (HitBlock() && hit.transform.tag == "damage"){
// hit.damage.damage(1);
//}
}
function getCam(){
var ts : Transform[] = transform.GetComponentsInChildren.<Transform>();
for (var t : Transform in ts)
if (t.gameObject.name == "Main Camera")
return t.gameObject;
}
function HitBlock() : boolean {
return Physics.Raycast(getCam().transform.position, getCam().transform.forward, hit, range, blockLayer);
}
function getCopn(){
for(i in FindObjectsOfType(typeof(computer))){
if(i.open)
return true;
Debug.Log(i);
Debug.Log(i.open);
}
return false;
}
function saveWorld(){
var allblocks : blockld[];
allblocks = FindObjectsOfType(typeof(blockld));
PlayerPrefs.SetInt("objects",allblocks.length);
var temp : GameObject;
PlayerPrefs.SetInt("saved",1);
GUI.Box(Rect(200,200,280,300),"saving, give me a sec...");
//SceneView.RepaintAll();
for(var i : int=0;i<allblocks.length;i++){
temp=allblocks[i].gameObject;
PlayerPrefs.SetFloat(i.ToString()+"_x",temp.transform.position.x);
PlayerPrefs.SetFloat(i.ToString()+"_y",temp.transform.position.y);
PlayerPrefs.SetFloat(i.ToString()+"_z",temp.transform.position.z);
PlayerPrefs.SetFloat(i.ToString()+"_rx",temp.transform.rotation.x);
PlayerPrefs.SetFloat(i.ToString()+"_ry",temp.transform.rotation.y);
PlayerPrefs.SetFloat(i.ToString()+"_rz",temp.transform.rotation.z);
PlayerPrefs.SetFloat(i.ToString()+"_rw",temp.transform.rotation.w);
PlayerPrefs.SetInt(i.ToString()+"_ID", allblocks[i].id);
if(temp.GetComponent.<Rigidbody>()){
PlayerPrefs.SetFloat(i.ToString()+"_vx",temp.GetComponent.<Rigidbody>().velocity.x);
PlayerPrefs.SetFloat(i.ToString()+"_vy",temp.GetComponent.<Rigidbody>().velocity.y);
PlayerPrefs.SetFloat(i.ToString()+"_vz",temp.GetComponent.<Rigidbody>().velocity.z);
PlayerPrefs.SetFloat(i.ToString()+"_ax",temp.GetComponent.<Rigidbody>().angularVelocity.x);
PlayerPrefs.SetFloat(i.ToString()+"_ay",temp.GetComponent.<Rigidbody>().angularVelocity.y);
PlayerPrefs.SetFloat(i.ToString()+"_az",temp.GetComponent.<Rigidbody>().angularVelocity.z);
}
}
var planet = GameObject.Find("planet0");
PlayerPrefs.SetFloat("PX",player.transform.position.x);
PlayerPrefs.SetFloat("PY",player.transform.position.y);
PlayerPrefs.SetFloat("PZ",player.transform.position.z);
PlayerPrefs.SetFloat("VX",player.GetComponent.<Rigidbody>().velocity.x);
PlayerPrefs.SetFloat("VY",player.GetComponent.<Rigidbody>().velocity.y);
PlayerPrefs.SetFloat("VZ",player.GetComponent.<Rigidbody>().velocity.z);
PlayerPrefs.SetFloat("p0_x",planet.transform.position.x);
PlayerPrefs.SetFloat("p0_y",planet.transform.position.y);
PlayerPrefs.SetFloat("p0_z",planet.transform.position.z);
PlayerPrefs.SetFloat("p0_vx",planet.GetComponent.<Rigidbody>().velocity.x);
PlayerPrefs.SetFloat("p0_vy",planet.GetComponent.<Rigidbody>().velocity.y);
PlayerPrefs.SetFloat("p0_vz",planet.GetComponent.<Rigidbody>().velocity.z);
PlayerPrefs.SetFloat("p0_rx",player.transform.rotation.x);
PlayerPrefs.SetFloat("p0_ry",player.transform.rotation.y);
PlayerPrefs.SetFloat("p0_rz",player.transform.rotation.z);
PlayerPrefs.SetFloat("p0_rw",player.transform.rotation.w);
PlayerPrefs.SetFloat("p0_ax",player.GetComponent.<Rigidbody>().angularVelocity.x);
PlayerPrefs.SetFloat("p0_ay",player.GetComponent.<Rigidbody>().angularVelocity.y);
PlayerPrefs.SetFloat("p0_az",player.GetComponent.<Rigidbody>().angularVelocity.z);
planet = GameObject.Find("planet1");
PlayerPrefs.SetFloat("p1_x",planet.transform.position.x);
PlayerPrefs.SetFloat("p1_y",planet.transform.position.y);
PlayerPrefs.SetFloat("p1_z",planet.transform.position.z);
PlayerPrefs.SetFloat("p1_vx",planet.GetComponent.<Rigidbody>().velocity.x);
PlayerPrefs.SetFloat("p1_vy",planet.GetComponent.<Rigidbody>().velocity.y);
PlayerPrefs.SetFloat("p1_vz",planet.GetComponent.<Rigidbody>().velocity.z);
PlayerPrefs.SetFloat("p1_rx",player.transform.rotation.x);
PlayerPrefs.SetFloat("p1_ry",player.transform.rotation.y);
PlayerPrefs.SetFloat("p1_rz",player.transform.rotation.z);
PlayerPrefs.SetFloat("p1_rw",player.transform.rotation.w);
PlayerPrefs.SetFloat("p1_ax",player.GetComponent.<Rigidbody>().angularVelocity.x);
PlayerPrefs.SetFloat("p1_ay",player.GetComponent.<Rigidbody>().angularVelocity.y);
PlayerPrefs.SetFloat("p1_az",player.GetComponent.<Rigidbody>().angularVelocity.z);
PlayerPrefs.Save();
}
}
在CmdSyncBlock中,我在代码运行时遇到此错误:
Object reference not set to an instance of an object
我做了什么:
添加了networkTransforms和NetworkIdentities
注册产卵预制品
如何在没有错误的情况下正确显示所有客户端上的这些对象?
答案 0 :(得分:1)
您需要在块预制件上安装网络标识脚本,然后在NetworkManager中将它们注册为可生成的预制件。
如果您不想使用Unity的默认NetworkManager,那么您也可以在代码上自行注册预制件。
有关详细信息,请参阅链接:http://docs.unity3d.com/Manual/UNetSpawning.html
另外,你不能syncvar游戏对象。相反,您应该在IDm和CmdBuild内部同步,根据同步ID定义要使用的块。