0

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

}

}
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Grant Zukel
  • 1,153
  • 2
  • 24
  • 48
  • 2
    I really think it would be worth your while refactoring your code to be pure C#. Its just going to be painful for you if you don't. – S.Richmond Sep 02 '14 at 03:12
  • ahh yea thats probably a good idea but all the tutorials are always in javascript. – Grant Zukel Sep 02 '14 at 03:42
  • Instead of destroying the player object, I'd re-position the location of the player and just reset the state of the player to whatever their default respawn state would be. Because its more than likely cheaper to just keep the same instance. Using Destroy is something that use when dealing with more disposable assets like enemies and items. – Terrance Sep 02 '14 at 19:24

1 Answers1

1

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(){
Rudolfwm
  • 689
  • 6
  • 15