Unity Multiplayer Respawn从Unityscript调用C#

时间:2014-09-02 02:53:38

标签: c# unity3d unityscript photon

我正在尝试使用我的子弹收集脚本删除并重新生成一个播放器,当它们的健康状况低于1.但是我调用unity c#函数的脚本不能正常工作,它说我试图调用的函数是

  

资产/级别/资源/ bulletCollision.js(27,16):BCE0019:   'SpawnMyPlayer'不是'UnityEngine.Component'的成员。

这也是重生杀死球员的正确方法吗?

NetworkManager.cs:

using UnityEngine;
using System.Collections;

public class NetworkManager : MonoBehaviour {
    public Camera standbyCamera;
    // Use this for initialization
    SpawnSpot[] spawnSpots; 
    void Start () {
        Connect ();
        spawnSpots = GameObject.FindObjectsOfType<SpawnSpot> ();
    }

    void Connect(){
        PhotonNetwork.ConnectUsingSettings ("1.0.0");
    }

    void OnGui(){
        Debug.Log ("OnGui" + PhotonNetwork.connectionStateDetailed.ToString ());
        GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString ());
    }
    // Update is called once per frame
    void OnJoinedLobby () {
        Debug.Log ("Joined Lobby");
        PhotonNetwork.JoinRandomRoom ();
        }
    void OnPhotonRandomJoinFailed(){
        Debug.Log ("Failed Join");
        PhotonNetwork.CreateRoom (null);
    }
    void OnJoinedRoom() {
        Debug.Log ("Joined Room");
        SpawnMyPlayer ();
    }
    void SpawnMyPlayer(){
        SpawnSpot mySpawnSpot = spawnSpots [ Random.Range (0, spawnSpots.Length) ];

        GameObject myPlayer = PhotonNetwork.Instantiate ("Player", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
        standbyCamera.enabled = false;

        ((MonoBehaviour)myPlayer.GetComponent("FPSInputController")).enabled = true;
        ((MonoBehaviour)myPlayer.GetComponent("PlayerCounters")).enabled = true;
        ((MonoBehaviour)myPlayer.GetComponent("Tankbody")).enabled = true;
        ((MonoBehaviour)myPlayer.GetComponent("tankMove")).enabled = true;
        ((MonoBehaviour)myPlayer.GetComponent("CharacterMotor")).enabled = true;
        myPlayer.transform.FindChild("Main Camera").gameObject.SetActive(true);

    }
}

bullet collision.js:

#pragma strict

var myClip: AudioClip;
var damage :float = 0;
var bullet_force: float = shoot.shootForce;


function OnCollisionEnter ( collision : Collision)
{

    Destroy(gameObject);

if(collision.transform.name ==("TankBody")){
    var hitCount = gameObject.Find("HitCount").GetComponent(GUIText);
    damage = Random.Range(10,30);
    PlayerCounters.playerHealth -= damage;  
    hitCount.text = "Hit: " + damage.ToString();

    AudioSource.PlayClipAtPoint(myClip, transform.position);

    if(PlayerCounters.playerHealth <0){
        Destroy(gameObject.Find("Player"));
        PlayerCounters.playerHealth = 0;
        PlayerCounters.playerKills += 1;
        var cs = GameObject.Find("CSharpGameObj");
        var script = cs.GetComponent("NetworkManager");
        script.SpawnMyPlayer();
    }

}

}

1 个答案:

答案 0 :(得分:1)

你的问题与Unity3d在传递中编译c#和javascript有关。 Javascript文件在之前编译 c#文件,因此无法找到您的c#类。

有一种解决方法!

您必须在assets文件夹中创建一个名为“Plugins”的文件夹,然后将您的c#脚本移动到该文件夹​​。它将在您的javascript文件之前编译。你应该发现你的JS脚本现在可以引用c#脚本。

在代码中:

    var cs = GameObject.Find("CSharpGameObj");
    var script :NetworkManager;
    script = cs.GetComponent("NetworkManager");
    script.SpawnMyPlayer();

顺便说一下,你必须公开SpawnMyPlayer函数,在它可以从外面访问之前,先将 public 添加到它的声明中:

    public void SpawnMyPlayer(){