C# 计数点太空射手联合会
我在GameController中的代码是:C# 计数点太空射手联合会,c#,unity3d,counting,C#,Unity3d,Counting,我在GameController中的代码是: public void AddScore(int newscore) { score += newscore; UpdateScore(); } void UpdateScore() { scoreText.text = "score " + score; } 我在destroyByContact中的代码: public GameController gameController; void OnTriggerEn
public void AddScore(int newscore)
{
score += newscore;
UpdateScore();
}
void UpdateScore()
{
scoreText.text = "score " + score;
}
我在destroyByContact中的代码:
public GameController gameController;
void OnTriggerEnter(Collider other)
{
if (other.tag =="boundary")
{
return;
}
Instantiate(explosion, transform.position, transform.rotation);
if (other.tag == "player")
{
Instantiate(playerexplosion, other.transform.position, other.transform.rotation);
}
gameController.AddScore(scoreValue);
Destroy(other.gameObject);
Destroy(gameObject);
}
并显示此错误:
错误CS1061:类型
GameController'不包含
AddScore'的定义,并且找不到类型为GameController'的扩展方法AddScore'(您是否缺少using指令或程序集引用?我不知道您的实际destroyByContact类。但我认为您可能没有声明或引用该对象
using UnityEngine;
using System.Collections;
public class destroyByContact : MonoBehaviour {
public GameObject explosion;
public GameObject playerExplosion;
public int scoreValue;
private GameController gameController;
void Start()
{
GameObject gameControllerObject = GameObject.FindWithTag("GameController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent<GameController>();
}
if (gameController == null)
{
Debug.Log("Cannot find 'GameController' script");
}
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("boundary"))
{
return;
}
if (explosion != null)
{
Instantiate(explosion, transform.position, transform.rotation);
}
if (other.tag == "Player")
{
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
gameController.GameOver();
}
gameController.AddScore(scoreValue);
Destroy(other.gameObject);
Destroy(gameObject);
}
使用UnityEngine;
使用系统集合;
公共类:单行为{
公开游戏对象爆炸;
公开游戏对象玩家爆炸;
公共价值;
私人游戏控制器;
void Start()
{
GameObject gameControllerObject=GameObject.FindWithTag(“GameController”);
if(gameControllerObject!=null)
{
gameController=gameControllerObject.GetComponent();
}
如果(gameController==null)
{
Log(“找不到'GameController'脚本”);
}
}
无效对撞机(对撞机其他)
{
如果(其他比较标记(“边界”))
{
返回;
}
if(爆炸!=null)
{
实例化(爆炸、变换.位置、变换.旋转);
}
如果(other.tag==“玩家”)
{
实例化(playerExplosion,other.transform.position,other.transform.rotation);
gameController.GameOver();
}
gameController.AddScore(scoreValue);
销毁(其他游戏对象);
摧毁(游戏对象);
}
}
请注意,在
start()
尝试获取GameController脚本的引用&如果未引用脚本,它将打印以下日志。哪里定义了scoreValue
?更新问题中的代码请向我们显示GameController类的定义签名。GameController是基类吗?如果是这样,那么您需要将您的控制器强制转换为定义了AddScore方法的相应控制器,如果这是一个特定的方法,而不是所有游戏控制器的通用方法。