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 awesomebuddy10 · Sep 09, 2018 at 05:43 PM · scripting problemscoreonmousedownscore system

How to create a score manager script involving awarding points from multiple objects?

I have many objects which award the user points dependent on which object has been tapped. So I thought of creating an enum for these objects, as they are of the same type and tried to use a switch statement to make it more efficient.

However, the problem is that I have created a script for each object where the scripts for these objects are nearly identical except for the line of code which awards the user points within the onmousedown() method for when the user taps an object, and I don't know how to put this all into one script to reduce clutter in my game. I'm not very good at coding which is why I'm having this trouble.

Also, the score is calculated within my main game controller script, so I'm calling the method which calculates the score in the scripts attached to each object to output the overall score.

Can someone show me how to efficiently do this? As I don't want to constantly add a new script to every new object I make.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by myzzie · Sep 09, 2018 at 05:57 PM

Without knowing what "difference" you want between the gameobjects, this is how you can add different values depending on which gameobject this script is attached to.

 public int scoreToGive;
 
 void OnMouseDown()
 {
     Scoremanager.AddScore(scoreToGive);
 }
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 awesomebuddy10 · Apr 04, 2019 at 02:48 PM 0
Share

Hi, thanks for the response. Sorry for being vague, but I've figured it out now.

avatar image
0

Answer by tormentoarmagedoom · Sep 09, 2018 at 06:05 PM

Good day.

As you also have a game controller script, the best way is:

In all objects. have exactly the same script, that the only thing that do is to call a funciton in the game controller script, with a parameter, so the method can know what object called it. This parameter can be the name of the object, or the tag of the object, or something for "classify" all objects that can be cliked. you will have 1 script with only 1 function controlling, everything.

For example, at the game controller do this:

  int TotalPoints;

 public void ObjectHasBeenClicked(string objectTag)
 {
  int pointsToAdd
 switch (objectTag)
         {
         case "Coin":
             pointsToAdd = 2;
             //Something 
             break;
          case "Ring":
             pointsToAdd = 2;
             //Something
             break;
          case "Diamond":
             pointsToAdd = 2;
             //Something
             break;
         }
    TotalPoints += pointsToAdd;
 }

So every time a object is clicked you need to call this method from the objectsScript giving the tag name of the object clicked.

 GameControllerScript.ObjectHasBeenClicked(gameobject.tag);


Bye!

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 awesomebuddy10 · Apr 04, 2019 at 02:50 PM 0
Share

Hi, thank you!! This was what I originally thought of and implemented, but it didn't work properly. I ended up putting all the if statements within the onmousedown() method which worked fine.

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

162 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

Related Questions

Central score counter for 3 score generating buttons 1 Answer

Score counter breaking after adding points 2 Answers

Can't increase my score after OnTriggerEnter occurs. 1 Answer

I want my score to reset back to 0 but keep my highscore saved 3 Answers

Incrementing score between two scripts 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