Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by S_George · Mar 14, 2019 at 04:38 PM · c#script.rigidbody2dvelocity

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xxmariofer · Mar 14, 2019 at 04:45 PM 0
Share

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();
  }
avatar image S_George xxmariofer · Mar 14, 2019 at 05:08 PM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image S_George · Mar 14, 2019 at 06:00 PM 0
Share

Thanks a lot, you acctualy saved a lot of nerves and shared your knowledge with me. Thank you in advance!

avatar image CybexGS S_George · Mar 14, 2019 at 06:12 PM 0
Share

You're very welcome :) I hope it works to your wishes.

avatar image S_George · Mar 14, 2019 at 06:27 PM 0
Share

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

avatar image S_George · Mar 15, 2019 at 07:33 AM 0
Share

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)

avatar image Hellium S_George · Mar 15, 2019 at 07:35 AM 0
Share
 score.text = "Score " + player.rb.position.y.ToString("0"); 
avatar image S_George Hellium · Mar 16, 2019 at 09:00 AM 0
Share

can you help me? https://answers.unity.com/questions/1612553/displaying-the-y-value-scoredisplaying-the-score-o.html

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

598 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is the difference between gameobject movement speed by velocity and calculating the speed with deltatime and magnitude ? 1 Answer

[C#, 2D] How do I apply force to a player using vector 3 velocity to move 1 Answer

I can't use Rigidbody2D in my scripts, I can't find rigidbody2d in my script 3 Answers

How to increase and decrease the animation speed? 1 Answer

Put a limit on velocity on the X axis 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges