类型或命名空间名称';得分';找不到
本文关键字:得分 找不到 命名空间 类型 | 更新日期: 2023-09-27 17:58:08
我不知道为什么,但我看到了这个错误。。我遵循了一个教程,到目前为止没有错过任何东西,但现在我已经学会了。
找不到类型或命名空间名称"Score"(是否缺少using指令或程序集引用?)
PlayerMotor.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlayerMotor : MonoBehaviour
{
private CharacterController controller;
private Vector3 moveVector;
private float speed = 10.0f;
private float verticalVelocity = 0.0f;
private float gravity = 12.0f;
private float animationDuration = 3.0f;
private bool isDead = false;
void Start () {
controller = GetComponent<CharacterController>();
}
void Update () {
if (isDead)
return;
if(Time.time < animationDuration)
{
controller.Move(Vector3.forward * speed * Time.deltaTime);
return;
}
moveVector = Vector3.zero;
if(controller.isGrounded)
{
verticalVelocity = -0.5f;
}
else
{
verticalVelocity -= gravity * Time.deltaTime;
}
// X - Left and Right
moveVector.x = Input.GetAxisRaw("Horizontal") * speed;
// Y - Up and Dow
moveVector.y = verticalVelocity;
// Z - Forward and Backward
moveVector.z = speed;
controller.Move (moveVector * Time.deltaTime);
}
public void SetSpeed(float modifier)
{
speed = 10.0f + modifier;
}
private void OnControllerColliderHit (ControllerColliderHit hit)
{
if (hit.point.z > transform.position.z + controller.radius)
Death();
}
private void Death()
{
isDead = true;
GetComponent<Score>().OnDeath();
}
}
Score.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
private float score = 0.0f;
private int difficultyLevel = 1;
private int maxDifficultyLevel = 10;
private int scoreToNextLevel = 10;
private bool isDead = false;
public Text scoreText;
void Update () {
if (isDead)
return;
if (score >= scoreToNextLevel)
LevelUp();
score += Time.deltaTime * difficultyLevel;
scoreText.text = ((int)score).ToString ();
}
void LevelUp()
{
if (difficultyLevel == maxDifficultyLevel)
return;
scoreToNextLevel *= 2;
difficultyLevel++;
GetComponent<PlayerMotor>().SetSpeed (difficultyLevel);
Debug.Log (difficultyLevel);
}
public void OnDeath()
{
isDead = true;
}
}
我猜你在这里提到的两个脚本都没有附加到同一个游戏对象上。快速解决方法是在PlayerMotor脚本中为分数脚本创建一个变量,然后将分数脚本拖动到编辑器中的变量中进行设置。或者你可以进行
GameObject.Find("Player").GetComponent<Score>().OnDeath();
或者你在玩家死亡时摧毁了游戏对象,在这种情况下什么都不会起作用。
我不是在开玩笑,我关闭了所有东西,重新打开它,它就工作了。
我完成