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 /
This question was closed Feb 12, 2018 at 09:00 PM by poppysilvers1997 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by poppysilvers1997 · Feb 12, 2018 at 07:24 PM · scripting problemscript.score system

Adding value from a clone object to score

So I'm working on kind of a peggle esk project. The way it works is a projectile bounces around hitting pegs, every time it hits a peg it adds 100 to the score and the peg disapears. If it hits two pegs it adds 110 on the second one, 3 pegs it adds 120, and so on.

public float value; public float increase; private float scoreM; // scoreM is the score modifier, this is what will be added to the score at the end void Start () { scoreM = value; // the score value starts at just the base score before increasing } void OnCollisionEnter2D(Collision2D col) { if (col.gameObject.tag == "peg") { scoreM = scoreM + increase; // every peg hit increases the scoreM by the increase value }

This works all fine and good, I have it printing the projectiles score value and it's going up for sure. My problem is I need to keep track of the score. I would just keep track of it on the projectile, but it's a clone, meaning every individual projectile will have it's own score, and then when it disapears the score it has will disapear with it. So my question is, is it possible for me to have one public variable that all my scripts can acces? If not, can the projectile add to a seperate scripts public int?

Thanks for any assistance! Sorry if I didn't give enough information, but any assistance is apreciated!

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

4 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by DevinW · Feb 12, 2018 at 07:31 PM

You could easily use a static float variable in a singleton.

Something like

 public static class PlayerData
 {
      public static int Score;
 }
 

using that you'd just do PlayerData.Score to reference it from anywhere in the codebase.

but that design pattern is really ugly and has a large margin for human errors. The eligant solution to data storage and clean references across the project can be found here. https://www.schellgames.com/blog/insights/game-architecture-with-scriptable-objects

Or in your own research on ScriptableObjects - there are a lot of good references out there.

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

Answer by Glurth · Feb 12, 2018 at 07:31 PM

This simplest solution for this would be to use a static class member.

 class ClassA{
 static float score;
 }

The static modifier means that ALL instances of ClassA will share the same single value for "score". If you make add the "public" modifier to the declaration of score,

 class ClassA{
 public static float score;
 }

then you can access it from outside class A, like so:

 ClassA.score += scoreIncrement;

Notice, you don't need an instance of ClassA, you can simply use the class name itself to access the score variable.

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

Answer by Mathijs_Bakker · Feb 12, 2018 at 07:48 PM

Is this wat you want?

     public class Projectilescore : MonoBehaviour
     {
         public float value; 
         public float increase; 
         // scoreM is the score modifier, this is what will be added to the score at the end.
         private float scoreM;  
         
         void Start () 
         { 
             // the score value starts at just the base score before increasing.
             scoreM = value;  
         }
 
         void OnCollisionEnter2D(Collision2D col)
         {
             if (col.gameObject.tag == "peg")
             {
                 // every peg hit increases the scoreM by the increase value.
                 scoreM += increase;
 
                 // Update the TotalScore:
                 TotalScore.Score += increase;
             }
         }
     }
 
     public static class TotalScore
     {
         public static float Score { get; set; }
         
         static TotalScore()
         {
             Score = 0f;
         }
 
         public static void Reset()
         {
             Score = 0f;
         }
     }



Above we are using a static class... but you could also use a singleton to keep track of game scoring.
I added a Reset method in TotalScore so you can reset the value when restarting your game.

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

Answer by poppysilvers1997 · Feb 12, 2018 at 09:00 PM

Thanks for all the help! The method I wen't with was simply applying it to a static int "points" with a text command reading static int points. Thanks for the help!

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

Follow this Question

Answers Answers and Comments

138 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

Related Questions

Layermask exception 0 Answers

How to Destroy the DontDestroyOnLoad() 2 Answers

When the mouse is moving and the player is walking why the player is stuttering ? 0 Answers

IEnumerator Loop on keypress 1 Answer

How to get all child objects under specific object ? 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