【发布时间】:2019-01-30 04:31:33
【问题描述】:
我正在统一制作篮球游戏,第一人称的球员能够将球射入篮筐,但我遇到了错误
Assets/Project/Scripts/GameController.cs(19,14):错误 CS0122: `Player.holdingBall' 由于其保护级别而无法访问
如何解决此类错误?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour {
public Player player;
public float resetTimer = 5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (player.holdingBall == false) {
resetTimer -= Time.deltaTime;
if (resetTimer <= 0) {
SceneManager.LoadScene("Game");
}
}
}
}
这是我的播放器脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
public GameObject ball;
public GameObject playerCamera;
public float ballDistance = 2f;
public float ballThrowingForce = 5f;
private bool holdingBall = true;
// Use this for initialization
void Start () {
ball.GetComponent<Rigidbody> ().useGravity = false;
}
// Update is called once per frame
void Update () {
if (holdingBall) {
ball.transform.position = playerCamera.transform.position + playerCamera.transform.forward * ballDistance;
if (Input.GetMouseButtonDown(0)) {
holdingBall = false;
ball.GetComponent<Rigidbody>().useGravity = true;
ball.GetComponent<Rigidbody>().AddForce(playerCamera.transform.forward * ballThrowingForce);
}
}
}
}
【问题讨论】: