So I have a simple cube player, and sometimes when the character falls of the edge, if they hit the side of the edge, a large NaN will appear on the screen instead of the score, and starts the game over. How do I prevent this from happening? Is it an error in the code?
NaN means “Not a Number”.
Can’t tell how without seeing the code that calculates the score, but it is an indication of an error in that calculation that result to invalid floating point number.
The Statistic is showing how far the player has gone on the plane. Its very much like Flappy Bird, in that the farther you travel, the higher the number.
Oh God that’s not what I meant… I meant just the script that manages the score text and calculates the value to show.
To post code, please don’t take pictures of your screen! Use the insert code button so that even long scripts look nice and neat. For example:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthScript : MonoBehaviour{
public DescriptionPanelController PanelControllerScript;
public ParticleSystem BloodEffects;
public Rigidbody Rigid;
public Animator Animations;
public AudioSource ZombieDeathSound;
public Component ControlScript;
public float HP;
public float StartHP; //original HP before damage
[Header("Player HP UI")]
public Image HPPanel;
public Text HPText;
// Use this for initialization
void Start ()
{
StartHP = HP;
UpdateHPUI();
PanelControllerScript = GameObject.Find("Canvas").GetComponent<DescriptionPanelController>();
}
// Update is called once per frame
void Update () {
}
public void TakeDamage(float DamageAmount, Vector3 DamagePosition)
{
HP -= DamageAmount;
BloodEffects.transform.LookAt(DamagePosition);
BloodEffects.Play();
if (HP < 0)
Die();
if(transform.name == "Player")
UpdateHPUI();
}
void Die()
{
if(ControlScript!=null)
Destroy(ControlScript);
transform.tag = "Untagged";
//since HealthScript is used on both player and enemies, only enemies should disable the description panel
if (transform.name != "Player")
{
Rigid.isKinematic = true;
Destroy(GetComponent<Collider>());
Animations.SetBool("Dead", true);
ZombieDeathSound.Play();
PanelControllerScript.HideDescriptionPanel();
}
}
void UpdateHPUI()
{
if (transform.name == "Player")
{
if (HP > 0)
HPText.text = HP.ToString();
else
HPText.text = "DEAD";
HPPanel.fillAmount = StartHP / HP;
}
}
}
I think that your scoretext should simply = player.transform.position.z. As far as I know, there’s no ToString() needed. If you want to round the number you get, you can use the Mathf.Round function.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Credits : MonoBehaviour {
public void Quit ()
{
Debug.Log("QUIT");
Application.Quit();
}
}
using UnityEngine;
public class EndTrigger : MonoBehaviour {
public Manager justManager;
void OnTriggerEnter ()
{
justManager.CompleteLevel();
}
}
using UnityEngine;
public class FollowPlayer : MonoBehaviour {
public Transform player;
public Vector3 offset;
// Update is called once per frame
void Update () {
transform.position = player.position + offset;
}
}
using UnityEngine;
using UnityEngine.SceneManagement;
public class Manager : MonoBehaviour {
bool gameHasEnded = false;
public float restartDelay = 1f;
public GameObject completeLevelUI;
public void CompleteLevel ()
{
completeLevelUI.SetActive(true);
}
public void EndGame ()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
Debug.Log("GAME OVER");
Invoke("Restart", restartDelay);
}
}
void Restart ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
using UnityEngine;
using UnityEngine.SceneManagement;
public class Menu : MonoBehaviour {
public void StartGame ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
using UnityEngine;
public class PlayerCollision : MonoBehaviour {
public PlayerMovement movement;
void OnCollisionEnter (Collision collisionInfo)
{
if (collisionInfo.collider.tag == "Obstacle")
{
movement.enabled = false;
FindObjectOfType<Manager>().EndGame();
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public Rigidbody rb;
public float forwardFORCE = 2000f;
public float sidewaysFORCE = 500f;
// Update is called once per frame
void FixedUpdate () {
rb.AddForce(0, 0, forwardFORCE * Time.deltaTime);
if (Input.GetKey("d") )
{
rb.AddForce(sidewaysFORCE * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a"))
{
rb.AddForce(-sidewaysFORCE * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (rb.position.y < -1f)
{
FindObjectOfType<Manager>().EndGame();
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public Rigidbody rb;
public float forwardFORCE = 2000f;
public float sidewaysFORCE = 500f;
// Update is called once per frame
void FixedUpdate () {
rb.AddForce(0, 0, forwardFORCE * Time.deltaTime);
if (Input.GetKey("d") )
{
rb.AddForce(sidewaysFORCE * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a"))
{
rb.AddForce(-sidewaysFORCE * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (rb.position.y < -1f)
{
FindObjectOfType<Manager>().EndGame();
}
}
}
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour {
public Transform player;
public Text scoreText;
// Update is called once per frame
void Update () {
scoreText.text = player.position.z.ToString("0");
}
}
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelComplete : MonoBehaviour {
public void LoadNextLevel ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
It looks like you completely skimmed by the “I meant just the script that manages the score text and calculates the value to show.” part. Did you try my suggestion in my previous post?