- Home /
Question by
perfectnumber · Jan 24, 2019 at 03:29 PM ·
unity5answersanswers.unity3d.com
Basketball Script Problem
I'm trying to make a basketball game but there's an issue `Player.holdingBall' is inaccessible due to its protection level. How do I fix it?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour {
public Player player;
public float resetTimer = 5f;
void Start () {
}
void Update ()
{
if (player.holdingBall == false)
{
resetTimer -= Time.deltaTime;
if (resetTimer <= 0)
{
SceneManager.LoadScene("Game");
}
}
}
}
Comment
Answer by tormentoarmagedoom · Jan 24, 2019 at 03:32 PM
Good day.
Its probably because at script player, the variable holdingBall is not defineed as public.
Only public variables can be reached/readed/writed by other scripts.
But I also see, you did not defined player variable. How this script knows what is "player". Its because player.holdingBall is static?
To be sure, please post also the player script.
Bye!
Answer by perfectnumber · Jan 27, 2019 at 07:27 PM
Here's my player script. I have private bool holdingBall = true;
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);
}
}
}
}