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 /
  • Help Room /
avatar image
1
Question by Alex_lemes · May 13, 2017 at 01:45 PM · textscorespawningfind-gameobjectother script

Problem with player Score

Hello,

I'm having issues with the player score for when they pick up collectables in my game.

Basically, in my game collectables only appear after the player travels a certain distance and are therefore spawned in with a script, now I want the players score to be kept within a game manager object.

Basics of what I need help with:

  • Player collides with coin obj

  • Coin script takes collision and stores the coins value

  • Coin script updates the score store within the game manager object

  • game manager script updates the score UI

There are multiple coin objects in the scene that are constantly spawning in

Can anyone help me with a script that would update the players score that's stored on the game manager object when they collide with the collectables, even when there are multiple collectables in the scene?

My code so far; C# :

 public class coin : MonoBehaviour {
 
     public GameObject gmScoreTxt;
     public int value = 1;
     private int count = 0;
     public int playerScoreNum = 0;
 
     public int setScore = 0;
 
     void Start()
     {
         gmScoreTxt = GameObject.Find("gameManager");
     }
 
     void OnTriggerEnter(Collider collision)
     {
         if (collision.gameObject.tag == "Player")
         {
             
             count = count + value;
             playerScoreNum = count;
             setScore = gmScoreTxt.GetComponent<playerScore>().score;
             setScore = playerScoreNum;
        
             Destroy(gameObject);
         }


GameManager:

 public class playerScore : MonoBehaviour {
 
     public int score = 0;
     public Text scoreTxt;
 
 
     void Update()
     {
         
         scoreTxt.text = score.ToString();
 
      }




Thank you!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by abeLincoln41 · May 14, 2017 at 07:14 AM

Your code looks pretty good so far. Have you tried running the game and seeing if it works? One comment I have right away is that you do not have to use the toString() method with the score you can simply use score.

 public Text scoreText;
 public long score;
 
 void Update () {
     scoreText.text = "Score: " + Mathf.Round(score); // Doesnt have to be exactly like this. 
 }

As for your coin class, you are setting setScore to equal something twice, so the first line in which you set the score to is unnecessary. PlayerScoreNum is also unnecessary because you can just use count and set the score equal to that. You also have to change your score in the score manager using the coin class. You can do this by creating an object of playerScore in the coin class. Make sure you drag the score manager that you create into the public field setScore in the coin class.

 playerScore setScore;

 if (collision.gameObject.tag == "Player")
      {
          setScore.score =+ value;
     
          Destroy(gameObject);
      }

I hope this works. If it does not feel free to let me know and I will do my best to help!

Comment
Add comment · Show 1 · 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 Alex_lemes · May 14, 2017 at 10:41 AM 0
Share

Hey, thanks for the reply @abeLincoln41.

I've tried this and it still doesn't want to work, as soon as I collide with one of the coins I get this error:

NullReferenceException: Object reference not set to an instance of an object coin.OnTriggerEnter (UnityEngine.Collider collision) (at Assets/scripts/coin.cs:29)

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

105 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

Related Questions

Spawn Object on Set Score 0 Answers

when i reload the scene the score number remains the same and doesnt revert back to zero 1 Answer

In updating some score text, why should we use the Update function instead of separate functions? 0 Answers

Help making a high score with player prefs/displaying it 1 Answer

Can't update score text - small error breaks the game (Space Shooter Tutorial) 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