Выпуск Unity Player Holdingball - PullRequest
       4

Выпуск Unity Player Holdingball

0 голосов
/ 30 января 2019

Я делаю баскетбольную игру в единстве, где игрок от первого лица может забить мяч в корзину, и я получаю ошибку

Активы / Проект / Скрипты /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);
            }
    }
}
}

1 Ответ

0 голосов
/ 30 января 2019
private bool holdingBall = true;

равно private и поэтому не может быть доступно с помощью

player.holdingBall

или сделать его public полем, таким как

public bool holdingBall = true;

недостаткомэто решение состоит в том, что вы могли бы установить значение в другом месте.

Так что лучше создать для него свойство public только для чтения, например

private bool holdingBall = true;

public bool HoldingBall
{
    get { return holdingBall; }
}

он может быть прочитан, но не установлен с помощью

if(!player.HoldingBall) 
{
    ...
}

. Вы также можете полностью пропустить приватное поле и использовать только такое свойство, как

public bool HoldingBall{ get; private set; }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...