我在GameController中的代码是:
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);
}
和unity显示此错误:
错误CS1061:键入GameController' does not contain a definition for
AddScore'并且找不到扩展方法AddScore' of type
GameController'(您是否缺少using指令或程序集引用?)
答案 0 :(得分:1)
我不知道你的实际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);
}
}
请注意
中的代码开始()强>
尝试获取 GameController 脚本&amp;的引用如果未引用该脚本,则将打印以下日志。