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 Tyler 2 · Nov 18, 2010 at 06:04 AM · variablescorestatic

Increasing a score value?

Hello, this is the script for a projectile in my game. I want to make it so that when Object A collides with an object with tag B the score is increased, to put it simply, why doesn't this work?

> function OnTriggerEnter (other :
> Collider) {
>     if (other.gameObject.CompareTag ("Wall"))

{

    Destroy (gameObject);
};
if (other.gameObject.CompareTag
 <p>("Enemy")) {
         Destroy (gameObject);
         Destroy (other.gameObject);
         {
     score += 1;<br>
     if (score == 5)
     {
       displayScore(score);
     }
         };}
          function displayScore(int score)</p>
 
 <p>{   GUI.Label(Rect(0, 0, 50, 50),
 score); }</p>

Comment
Add comment · Show 1
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 Proclyon · Nov 18, 2010 at 07:59 AM 1
Share

The indentation and formatting of your post is all messed up, I unfortunatly don't have the permission to edit it for you so I would kindly ask you , or another with the permission to do so. And no I sadly don't have the answer for you

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bravini · Nov 18, 2010 at 11:25 AM

static var Score : int;

now you can access this variable from any script by using scriptname.Score, being scriptname the name of the script you declared the variable into.

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 Bravini · Nov 18, 2010 at 11:28 AM 0
Share

also, if your score var is in a script attached to the projectile, you gonna have multiple scripts in the scene, which will be destroyed along with the projectile when they collide. Put the score in the player or any other object you know that wont be destroyed.

other solution would be to use http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html

avatar image
0

Answer by jameslee0227 · Nov 18, 2010 at 08:08 AM

do you want show score?

recommand below

{

int score = 0;

function OnTriggerEnter ( )

...

score++;

...

}

void OnGUI() { if (score == 5)

{

     GUI.Label(Rect(0, 0, 50, 50), score); 

}

}

}

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

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

Accessing Other Scripts Without Using Static Variables 1 Answer

Make a trigger change a static variable 1 Answer

Why is static typing required for access of public JS function or var in Standard Assets 1 Answer

Static array variable 1 Answer

Will static variables not work if negative? 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