Hi, im trying to add score to my game, and i get this error message even though i think i did everything right. Where did I mess up? The error is on score.text = "Score: " + scoreValue
Here is my code:
public class BallShooter : MonoBehaviour
{
// Variables
Rigidbody rb;
public GameObject Ball;
public Transform spawnPoint;
float holdstartTime = 0f;
public float speed = 500;
private bool onGround = false;
public static int scoreValue = 0;
Text score;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
score = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
float delta = Time.time - holdstartTime;
float adjustedSpeed = speed * delta;
score.text = "Score: " + scoreValue;
if (Input.GetMouseButtonDown(0))
{
holdstartTime = Time.time;
}
if (Input.GetMouseButtonUp(0) && onGround)
{
rb.AddForce(Vector3.forward * adjustedSpeed);
rb.AddForce(Vector3.up * adjustedSpeed);
onGround = false;
}
}
// What happens when ball collides
private void OnCollisionEnter(Collision collision)
{
// If collision is with Wall
if(collision.gameObject.tag == "Wall")
{
transform.position = new Vector3(0, .5f, -5);
}
// If collision is with center
if (collision.gameObject.tag == "TargetCenter")
{
transform.position = new Vector3(0, .5f, -5);
scoreValue += 200;
}
if (collision.gameObject.tag == "TopTarget")
{
transform.position = new Vector3(0, .5f, -5);
scoreValue += 50;
}
if (collision.gameObject.tag == "MiddleTarget")
{
transform.position = new Vector3(0, .5f, -5);
scoreValue += 100;
}
// If collision is with floor
if (collision.gameObject.tag == "Floor")
{
onGround = true;
}
}
// Spawn function
void OnBecameInvisible()
{
GameFinished();
}
public void GameFinished()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}