为什么我不能访问此枚举器



我希望能够访问一个枚举器,我在名为"Rocket"的脚本中使用该枚举器来确定计时器何时启动。但是,当我尝试访问该变量时,它显示Assets\Scripts\Timer.cs(19,20(:error CS0122:"Rocket.state"由于其保护级别而无法访问。我试过公开枚举器,但这并没有改变任何事情。它不会让我把它变成静态的。

public class Timer : MonoBehaviour
{
public Text timer;
public float startTime;
void Start()
{
startTime = Time.time;
}
void Update()
{
if (Rocket.state == State.alive)
{
float t = Time.time - startTime;
string minutes = ((int)t / 60).ToString(); 
string seconds = (t % 60).ToString("f2"); 
timer.text = minutes + ":" + seconds;
}
}
}

第14行是我尝试访问枚举器的地方

public class Rocket : MonoBehaviour
{
public float rcsThrust = 100f; //Rotation
public float mainThrust = 100f; //Upwards Thrust
public float levelLoadDelay = 2f; //Delay between resetting or loading a new level
[SerializeField] AudioClip mainEngine;
[SerializeField] AudioClip Success;
[SerializeField] AudioClip Death; //Sounds
[SerializeField] ParticleSystem mainEngineP;
[SerializeField] ParticleSystem SuccessP;
[SerializeField] ParticleSystem DeathP; //Particles
enum State { Alive, Dying, Transcending }
State state = State.Alive; //Whether the rocket is dead or alive
Rigidbody rigidBody;
AudioSource audioSource;
public int LevelNumber;

// Use this for initialization
void Start()
{
rigidBody = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();

}
// Update is called once per frame
void Update()
{
if (state == State.Alive) //Allows you to control the rocket if the state is alive
{
RespondToThrustInput();
RespondToRotateInput();
}
}
void OnCollisionEnter(Collision collision) //If the rocket collides with an object, it will either die or win, depending on what the object was
{
if (state != State.Alive)
{
return;
}
switch (collision.gameObject.tag)
{
case "Friendly":
break;
case "FInish":
StartSuccessSequence(); 
break;
default:
StartDeathSequence();
break;
}
}
private void StartDeathSequence() //Playing sounds and particles on death
{
state = State.Dying;
audioSource.Stop();
audioSource.PlayOneShot(Death);
DeathP.Play();
Invoke("RestartLevel", levelLoadDelay);
}
private void StartSuccessSequence() //Playing sounds and particles on success
{
state = State.Transcending;
audioSource.PlayOneShot(Success);
SuccessP.Play();
TimerController.instance.BeginTimer();
Invoke("LoadNextScene", levelLoadDelay);
}
private void RestartLevel()
{
SceneManager.LoadScene(LevelNumber);
}
private void LoadNextScene() //Loads success screen
{
SceneManager.LoadScene("victoryscene");
}
private void RespondToThrustInput()
{
if (Input.GetKey(KeyCode.W)) // can thrust while rotating
{
ApplyThrust();
}
else
{
audioSource.Stop();
mainEngineP.Stop();
}
}
private void ApplyThrust()
{
rigidBody.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);
if (!audioSource.isPlaying) // so it doesn't layer
{
audioSource.PlayOneShot(mainEngine);
}
mainEngineP.Play();
}
private void RespondToRotateInput()
{
rigidBody.freezeRotation = true;
float rotationThisFrame = rcsThrust * Time.deltaTime;
if (Input.GetKey(KeyCode.D))// D and A are swapped around because the rocket model is rotated 180 degrees
{
transform.Rotate(Vector3.forward * rotationThisFrame);
}
if (Input.GetKey(KeyCode.A))// 'if' is used instead of 'elseif' because it will still be rotating to the left if 'elseif' is used
{
transform.Rotate(-Vector3.forward * rotationThisFrame);
}
rigidBody.freezeRotation = false;
}

}

这是包含枚举器的脚本

Rocket.state是一个私有值,这意味着你不能像编码那样在其他类中使用它

if (Rocket.state == State.alive)
{
float t = Time.time - startTime;
string minutes = ((int)t / 60).ToString(); 
string seconds = (t % 60).ToString("f2"); 
timer.text = minutes + ":" + seconds;
}

你必须设置";Rocket.state;像";公共浮动级别LoadDelay";所以把它改成:

Public State state = State.Alive;

最新更新