0

我正在尝试使用我的子弹收集脚本在玩家的生命值低于 1 时删除并重新生成玩家。但是我调用 unity c# 函数的脚本不太正常,它说我试图调用的函数是

资产/关卡/资源/bulletCollision.js(27,16):BCE0019:“SpawnMyPlayer”不是“UnityEngine.Component”的成员。

这也是重生被杀玩家的正确方法吗?

网络管理器.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);

    }
}

子弹碰撞.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();
    }

}

}
4

1 回答 1

1

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

有办法解决这个问题!

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

在代码中:

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

顺便说一句,您必须将 SpawnMyPlayer 函数公开,方法是在其声明中添加public,然后才能从外部访问它:

    public void SpawnMyPlayer(){
于 2014-09-02T07:08:54.360 回答