如何使用c#统一在另一个脚本中使用一个脚本的一个函数
how to use one function of a script in another script in unity using c#
我正在用 c# 统一制作游戏
我想在另一个脚本中使用一个脚本的函数
第一个脚本名称玩家控制器
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public Vector2 moving = new Vector2();
public int Bulletlimit = 0;
public int MaxBulletlimit = 3;
public Bullet bullet;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
moving.x = moving.y = 0;
if (Input.GetKey ("right")) {
moving.x = 1;
} else if (Input.GetKey ("left")) {
moving.x = -1;
}
if (Input.GetKey ("up")) {
moving.y = 1;
} else if (Input.GetKey ("down")) {
moving.y = -1;
}
if (Input.GetKey ("s")) {
BulletShot();
}
}
public void BulletShot(){
if(Bulletlimit < MaxBulletlimit)
{
Bullet clone = Instantiate (bullet, transform.position, Quaternion.identity) as Bullet;
Bulletlimit = Bulletlimit + 1;
}
}
public void BulletCount()
{
Bulletlimit = Bulletlimit - 1;
}
}
第二个脚本名称 Bullet Destroy
using UnityEngine;
using System.Collections;
public class BulletDestroy : MonoBehaviour {
private Player player;
private PlayerController playerController;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void OnTriggerEnter2D(Collider2D target){
if (target.gameObject.tag == "Deadly") {
Destroy (gameObject);
}
}
public void OnCollisionEnter2D(Collision2D target){
if (target.gameObject.tag == "Deadly") {
Destroy (gameObject);
}
}
}
如何在 if 条件下从 playercontroller 脚本调用 BulletCount 函数到 BulletDestroy 脚本
如果初学者不了解所有 C# 过程,您可以使用高效的 C# 方式或推荐给初学者的 Mono 方式。
第一个是继承另一个 Class。我不会讨论这个,因为如果你不知道它是如何工作的,它可能会在未来给你带来一些问题。
第二个是通过从播放器获取组件。如果在您的游戏中,玩家被标记为玩家,那么让我们使用该参考。或者至少是 Player GameObject 的名称。
PlayerController myCharactersScript;
void Start(){
myCharactersScript = GameObject.Find("myCharactersName").GetComponent<PlayersController>();
}
public void OnTriggerEnter2D(Collider2D target){
if (target.gameObject.tag == "Deadly") {
Destroy (gameObject);
myCharactersScript.BulletCount();
}
}
我建议您阅读继承以获得良好的编码实践,因为它是 C# 的主要核心之一。仅供参考,您只能继承一次。
我正在用 c# 统一制作游戏 我想在另一个脚本中使用一个脚本的函数
第一个脚本名称玩家控制器
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public Vector2 moving = new Vector2();
public int Bulletlimit = 0;
public int MaxBulletlimit = 3;
public Bullet bullet;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
moving.x = moving.y = 0;
if (Input.GetKey ("right")) {
moving.x = 1;
} else if (Input.GetKey ("left")) {
moving.x = -1;
}
if (Input.GetKey ("up")) {
moving.y = 1;
} else if (Input.GetKey ("down")) {
moving.y = -1;
}
if (Input.GetKey ("s")) {
BulletShot();
}
}
public void BulletShot(){
if(Bulletlimit < MaxBulletlimit)
{
Bullet clone = Instantiate (bullet, transform.position, Quaternion.identity) as Bullet;
Bulletlimit = Bulletlimit + 1;
}
}
public void BulletCount()
{
Bulletlimit = Bulletlimit - 1;
}
}
第二个脚本名称 Bullet Destroy
using UnityEngine;
using System.Collections;
public class BulletDestroy : MonoBehaviour {
private Player player;
private PlayerController playerController;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void OnTriggerEnter2D(Collider2D target){
if (target.gameObject.tag == "Deadly") {
Destroy (gameObject);
}
}
public void OnCollisionEnter2D(Collision2D target){
if (target.gameObject.tag == "Deadly") {
Destroy (gameObject);
}
}
}
如何在 if 条件下从 playercontroller 脚本调用 BulletCount 函数到 BulletDestroy 脚本
如果初学者不了解所有 C# 过程,您可以使用高效的 C# 方式或推荐给初学者的 Mono 方式。
第一个是继承另一个 Class。我不会讨论这个,因为如果你不知道它是如何工作的,它可能会在未来给你带来一些问题。
第二个是通过从播放器获取组件。如果在您的游戏中,玩家被标记为玩家,那么让我们使用该参考。或者至少是 Player GameObject 的名称。
PlayerController myCharactersScript;
void Start(){
myCharactersScript = GameObject.Find("myCharactersName").GetComponent<PlayersController>();
}
public void OnTriggerEnter2D(Collider2D target){
if (target.gameObject.tag == "Deadly") {
Destroy (gameObject);
myCharactersScript.BulletCount();
}
}
我建议您阅读继承以获得良好的编码实践,因为它是 C# 的主要核心之一。仅供参考,您只能继承一次。