• 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 OctoSloths · Jan 23, 2015 at 03:32 AM · c#ontriggerenterscorefloatint

Adding coin score to multiplied value

I have a script that causes the score that goes up by 100 every second, and a script that should add 1000 points every coin collided with. Both scripts run, however when the player collides with the coin, 1000 points isn't added to the current score.

Here is my script for score per second:

 using UnityEngine;
 using System.Collections;
 public class ScoreperSec : MonoBehaviour {
     public int startingScore = 0;
     public int currentScore = 0;
     public GUIText scoregui;
     private float time;
     public static int score;
 void Update (){
         time+=Time.deltaTime;
         //currentScore += (int)time;
         print ((int)time);
         score = (int)time * 100;
         //(int)time * 100 == score;
         scoregui.text = "Score: "+ score;
 }
     }

Here is my script for the coins that should add 1000 points each:

 using UnityEngine;
 using System.Collections;
 
 public class coins : MonoBehaviour {
 
     // Use this for initialization
     void OnTriggerEnter(Collider coll) {
             GameObject addScore = GameObject.Find("GUI");
             ScoreperSec scoreScript = addScore.GetComponent<ScoreperSec>();
         if (coll.gameObject.tag == "Player"){
             ScoreperSec.score += 100000;
             Destroy(this.gameObject);
         }
     }
 
 }
 

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
3
Best Answer

Answer by shriya · Jan 23, 2015 at 05:04 AM

Hi,

Got it working.Check below the scripts

 using UnityEngine;
 using System.Collections;
 public class ScoreperSec : MonoBehaviour {
     public int startingScore = 0;
     public int currentScore = 0;
     public GUIText scoregui;
     private float time;
     public static int score;
     public static int localscore;
     void Update (){
         time+=Time.deltaTime;
         print ((int)time);
         localscore = (int)time * 100;
         score = localscore + coins.coinscore;
         Debug.Log ("total score "+score);
 
 //        scoregui.text = "Score: "+ score;
     }
 }


this is the coin one using UnityEngine; using System.Collections;

 public class coins : MonoBehaviour {
     public static int coinscore;
     // Use this for initialization
     void OnTriggerEnter(Collider coll) {
         Debug.Log ("Trigger enter");
         if (coll.gameObject.tag == "Player"){
             coinscore += 1158;
             Debug.Log ("coins score"+ScoreperSec.score);
             Destroy(this.gameObject);
         }
     }
     
 }
 

Hope it helps :)

Comment
Add comment · Show 2 · 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 OctoSloths · Jan 23, 2015 at 05:07 AM 0
Share

Thank you so much!!! :D

avatar image shriya · Jan 23, 2015 at 05:12 AM 0
Share

Happy to help :)

avatar image
0

Answer by Redeemer86 · Jan 23, 2015 at 03:58 AM

Coins script ... Line 11

scoreScript.score += 100000;

Red

Comment
Add comment · Show 14 · 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 OctoSloths · Jan 23, 2015 at 04:06 AM 0
Share

I made this change and now I get the error: "Assets/Scripts/coins.cs(12,37): error CS0176: Static member `ScoreperSec.score' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d"

After receiving this error, I deleted the term static in the score script in this line: " public static int score;" and now the coin does not get destroyed, nor does it add points to the score.

avatar image shriya · Jan 23, 2015 at 04:16 AM 0
Share

Are you sure you have given the tag of player in inspector?

avatar image OctoSloths · Jan 23, 2015 at 04:18 AM 0
Share

Yes I'm sure. D:

avatar image OctoSloths · Jan 23, 2015 at 04:26 AM 0
Share

I tested it again and go this error message?:

"NullReferenceException: Object reference not set to an instance of an object coins.OnTriggerEnter (UnityEngine.Collider coll) (at Assets/Scripts/coins.cs:12)"

avatar image shriya · Jan 23, 2015 at 04:30 AM 0
Share

I just now implemented your code and its working fine. Your error is because of ScoreperSec scoreScript = addScore.GetComponent(); this line. simply comment these lines .you don't even use them

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Multiply float by int? 2 Answers

Can I create a list with an int/float and a string? C# 2 Answers

C# Do something every 100 points? 1 Answer

Have a problems with a values 1 Answer

4.6 Slider round to int? 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