Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 john 2 · Aug 13, 2010 at 01:31 PM · scorepoint

Score not adding?

when i run into an object that adds to our score in my game it disappears like it's supposed to ,but my score just stays at 0. Could someone please help me?

My Scoring Script:

var score = 0;

print("Score: "+score);

function OnTriggerEnter( hit : Collider ) {

if(hit.Object.tag == "player") { score += 5; Destroy(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

3 Replies

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

Answer by skovacs1 · Aug 25, 2010 at 03:02 PM

What your script is doing:

//Declare a score variable within this script var score = 0; //Print the "Score: 0" as the score variable was just created and set to 0 print("Score: "+score);

//When someone enters the trigger volume function OnTriggerEnter( hit : Collider ) { //If the .Object of the collider (shouldn't this be .gameObject?) is tagged "player" if(hit.Object.tag == "player") { //Add 5 to the score variable local to this script score += 5; //Destroy the gameObject to which this script is attached Destroy(gameObject); } }

Like PixelMaster said, if you declared score to be static, that means that all instances of your script will share the same score variable, which will make it so that the score will increase when the player enters the volume of any of the objects to which this script is attached.

However, like AliAzin said, you should store your score in a separate script which you then access so that objects with different scripts can still access your score.

If you are going by your print statements according to the code above, then they will be wrong because they don't print the score after it has been changed.

What you probably want:

foo.js attached to another gameObject tagged "bar"

var score : int = 0;

function Start() { print("Score: "+score); }

//So that you can print your score, etc. function AddScore(amount : int) { score += amount; print("Score: "+score); }

your script attached to your triggers

var scoreManager: Component = GameObject.FindWithTag("bar").GetComponent("foo");

function OnTriggerEnter(){ scoreManager.AddScore(5); Destroy(gameObject); }

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 john 2 · Aug 25, 2010 at 03:24 PM 0
Share

Thanks! I got it working!

avatar image
0

Answer by AliAzin · Aug 13, 2010 at 02:10 PM

because score is a local variable of each object. If you want to add score you should have a score manager object and each time add the score to it. like this:

var scoreManager : GameObjecy;

function OnTriggerEnter(){ scoreManager.GetComponent("yourManagerScriptName").score += 5;

}

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 pixelmixer · Aug 13, 2010 at 02:42 PM

You could also store the score as a Static variable so that the score persists even after the object is destroyed.

static var score = 0;

print("Score: "+score);

function OnTriggerEnter( hit : Collider ) { if(hit.Object.tag == "player") { score += 5; Destroy(gameObject);

If you go with then you will need to reset that static variable if you ever restart the current scene like the following: (In the same script file as your define the static variable)

function Awake() {
score = 0;
}

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 john 2 · Aug 13, 2010 at 05:58 PM 0
Share

is there anything else special i can do or equip it to anything special, because it still doesn't work.

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

No one has followed this question yet.

Related Questions

How do I make a score script based on the time the player is alive? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Unity Game Score Script 1 Answer

How to go about purchasing objects with player score/points? 1 Answer

Multiplayer ScoreBoard not Updating Correctly 0 Answers


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