- Home /
Help With Score Count Really need anyone to please help
The score is not displayed can you please help me with this however I have Added Script and text but still it does not counts Here is the script of Player The Pickup Script is separately attached to pickups
The PlayerController Script
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
public float jumpHeight;
public Text countText;
public Text winText;
public int numPickups;
private Rigidbody rb;
private int count;
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winText.text = "";
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
if (Input.GetKeyDown("space"))
{
Vector3 jump = new Vector3(0.0f, jumpHeight, 0.0f);
rb.AddForce(jump);
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= numPickups)
{
winText.text = "You win!";
}
}
}
The Pickup Script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PickUpScript : MonoBehaviour {
public GameObject PickUp;
void OnTriggerEnter(Collider col)
{
PickUp = GameObject.FindGameObjectWithTag("PickUp");
if (gameObject != null)
{
// Do something
Destroy(gameObject);
}
}
}
Answer by DevManuel · Feb 19, 2021 at 06:53 AM
Ok, In your script I don't see where you are changing the count-var. Have you forgotten to post this code? Then I would add some debug to see if only the var changes and the text is not updating. Your count-var is private, so you don't have access from another script to change the value. Maybe you create a function which handles the counter:
public void counter(){
count++;
Debug.Log("count: "+count);
SetCountText();
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
Debug.Log("counterText: "+ countText.text);
if (count >= numPickups)
{
winText.text = "You win!";
}
}
and in your PickupScript:
public PlayerController controller; // set it in the inspector
void OnTriggerEnter(Collider col)
{
Debug.Log("triggered");
PickUp = GameObject.FindGameObjectWithTag("PickUp");
if (gameObject != null)
{
// Do something
Debug.Log("counting...");
controller.counter();
Destroy(gameObject);
}
}
I hope I could help you, and happy coding.
Answer by alphatechexpert · Feb 19, 2021 at 09:55 AM
@DevManuel I am really sorry with my words But It did not work Please write what ever is needed in this script to update my score on screen and than post the script over here so I can use it The Script
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
public float jumpHeight;
public Text countText;
public Text winText;
public int numPickups;
private Rigidbody rb;
private int count;
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winText.text = "";
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
if (Input.GetKeyDown("space"))
{
Vector3 jump = new Vector3(0.0f, jumpHeight, 0.0f);
rb.AddForce(jump);
}
}
public void counter()
{
count++;
Debug.Log("count: " + count);
SetCountText();
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= numPickups)
{
winText.text = "You win!";
}
}
}
I would be very very grateful and thankful if you would type the missing codes to update the score on screen and what ever is missing
you want to count the pickups!? Am I right? Then I could write your code...
Yes Yes Please I would be very thankful
Ok, I will do it. After school I will write the code and test it in Unity
Your answer
![](https://koobas.hobune.stream/wayback/20220613025431im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Need help in typing project 0 Answers