- Home /
Displaying the y value score,Displaying the score of the Y value
Please help, what can i do? i am newbie to coding (1 week) ,Imade a doddle jump like game and want to add score to it. I want the score to display the Y position of the player. Here is my code
ScoreScript
using UnityEngine;
using UnityEngine.UI;
public class scorescript : MonoBehaviour
{
public static int ScoreValue = 0;
Text score;
void Start()
{
score = GetComponent<Text>();
}
void Update()
{
score.text = "Score " + ScoreValue;
}
}
Player script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rigidbody : MonoBehaviour
{
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
Vector3 v3Velocity = rb.velocity;
GetComponent<scorescript>();
}
void Update()
{
scorescript.scoreValue += rb.velocity.y;
}
}
Whats wrong, please help me i am newbie to unity.
change the update to this
void Update()
{
scorescript.scoreValue = rb.position.y;
}
and the other update to this
void Update()
{
score.text = "Score " + ScoreValue.ToString();
}
now it says Assets\scripts\rigidbody.cs(27,34): error CS0266: Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?)
what can i do?
Answer by CybexGS · Mar 14, 2019 at 05:44 PM
Since you're new to programming I'd like to go over your code a bit more than just making it work, so you don't develop bad habbits. I hope you're ok with that.
Lets start with the Player script:
First, class names start with a upper case letter and try not giving your classes names that do already exist.
I would suggest calling your player script actually just Player.
public class Player : MonoBehaviour
{
...
I would remove any dependencies on your score script from the player calss so 'Player.cs' can work independently of the score script without causing errors or bugs.
That means removing GetComponent<scorescript>();
from Start()
and removing scorescript.scoreValue += rb.velocity.y;
from Update()
.
Make the 'rb' field public so you can access it from the other script. public Rigidbody rb;
Now to the Score script:
Same rule for its name, first letter is upper case and i personally would give it a more descriptive name. Let's call it PlayerScore for now.
public class PlayerScore : MonoBehaviour
{
...
You can get completely rid of the public static int ScoreValue = 0;
field.
But just so you know: variable names always start with lower case and to declare this one variable field as static
is completely unnecessary.
Make a field to reference your Player class instead. It can be private because it will be used just in this class itself.
private Player player;
And in the Start()
method set the player reference;
void Start ()
{
player = GetComponent<Player>();
...
Now in your Update()
method just do:
score.text = "Score " + player.rb.position.y.ToString();
Your PlayerScore script will now automatically, every frame, access the Player script, read its rigidbodys Y position and display it as text. .ToString()
is needed to convert the float it recieves into an actual string value.
Hope this helps.
Thanks a lot, you acctualy saved a lot of nerves and shared your knowledge with me. Thank you in advance!
You're very welcome :) I hope it works to your wishes.
umm..
i have another problem...
There is an error: NullReferenceException: Object reference not set to an instance of an object PlayerScore.Update () (at Assets/scripts/PlayerScore.cs:24)
is this a problem with the script? or i forgot to do something outside the script
ok i figured out how to solve this, thanks any way I am very happy what i just done, but if its possible, how to make the score hexizecimal(not 91,439478 , 91 ins$$anonymous$$d)
score.text = "Score " + player.rb.position.y.ToString("0");