我不知道为什么,但我看到被抛出这个错误..我遵循了一个教程并且到目前为止没有遗漏任何东西,但现在我已经到了这里。
找不到类型或命名空间名称“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;
}
}
答案 0 :(得分:0)
我猜你在这里提到的两个脚本都没有附加到同一个游戏对象上。 快速解决此问题的方法是在PlayerMotor脚本中为score脚本创建一个变量,然后将得分脚本拖到编辑器中的变量中进行设置。 或者你可以 做
GameObject.Find("Player").GetComponent<Score>().OnDeath();
或者你可能会在死亡时摧毁玩家的游戏对象,在这种情况下,什么都行不通。
答案 1 :(得分:0)
我不是晚上开玩笑,我关闭了所有东西,重新打开它并且它有效。
我已经完成了