简体   繁体   中英

Unity Multiplayer Respawn Calling C# from Unityscript

I'm trying to use my bullet collection script to delete and respawn a player when their health drops below 1. However my script to call the unity c# function is not quite working right, it says that the function im trying to call is

Assets/Levels/Resources/bulletCollision.js(27,16): BCE0019: 'SpawnMyPlayer' is not a member of 'UnityEngine.Component'.

Also is this a proper way to respawn a killed player?

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();
    }

}

}

Your problem has to do with Unity3d compiling c# and javascript in passes. Javascript files are compiled before c# files, and hence cannot find your c# class.

There is a way around this!

You have to create a folder called 'Plugins' within your assets folder, and move your c# script to that folder. It will be compiled before your javascript file. And you should find your JS script can now refer to the c# script.

In code:

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

By the way you must make the SpawnMyPlayer function public, by adding public to its declaration before it can be accessed from the outside:

    public void SpawnMyPlayer(){

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM