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 Michael_glu · Nov 05, 2013 at 07:09 PM · score systemglobal variable

My scoring system doesn't work

I have made 2 separate scripts. I want to show user that the score is increased when he shoots a robot. Here is my code:

For displaying the score: var LivesLeft: int = 5; static var score = 0; // the variable score is supposed to be global now

 function Update () 
 {
     if (LivesLeft < 0)
     {
         print ("dead");
     }
 
 }
 
 function OnCollisionEnter (col: Collision)
 {
     if (col.gameObject.tag == "Bullet")
     {
         LivesLeft = LivesLeft - 1;
     }
 }
 
 function OnGUI ()
 {
     GUI.Label (Rect (10,10,250,70), "Lives Left " + LivesLeft);
     GUI.Label (Rect (20,20,400,80), "Score: " + score); // display score
 }

for bullet: var bullet: GameObject; var location: Transform; var explosion: GameObject; var BulletSpeed = 5;

 function Update () {
 transform.Translate (Vector3.forward * BulletSpeed);
 }
 function Explode ()
 {
         Instantiate (explosion, location.transform.position, location.transform.rotation );
         Destroy (bullet);
 }
 
 function OnCollisionEnter (col: Collision)
 {
 
     if (col.gameObject.tag == "Robot") //if bullet collides with robot
     {
         score = score + 100; //score should be used, because it is global. When collision happens, 100 is added
     }
     Explode ();
 }



It gives me an error message saying "Unknown identifier 'score'"

Comment
Add comment · Show 9
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 Stormizin · Nov 05, 2013 at 07:21 PM 0
Share

It's missing some script parts formated as code, can you please put the entire error message and edit your question?

avatar image ozturkcompany · Nov 05, 2013 at 07:32 PM 0
Share

What happens if you delete the static front of that score?

avatar image Michael_glu · Nov 06, 2013 at 07:21 PM 0
Share

that's all i have for scoring system, and i thought that static is supposed to make it global

avatar image ozturkcompany · Nov 06, 2013 at 07:50 PM 0
Share

Wait a $$anonymous$$ute! You are using two different scripts right? And score is only defined in one script and you are trying to get the variable in one of the script to another right?Well you didn't do anything to call it?

avatar image Seth-McCumber · Nov 06, 2013 at 09:13 PM 0
Share

Static Is Bad, Use GetComponent

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by meat5000 · Nov 05, 2013 at 07:23 PM

static var score = 0;

Not properly defined.

 static var score : int = 0;

If this doesn't help, I'm going to guess that you need to change your script execution order.

If the static variable is coming up Unknown it is highly likely that a script is trying to access it before it has been declared. You need to make absolutely sure that the variable is created and initialised before anything tries to access it. Make the script run first in SEO or declare the variable in a script that you know runs before the rest.

Comment
Add comment · Show 5 · 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 ozturkcompany · Nov 05, 2013 at 07:30 PM 1
Share

Would it make a difference? Its a float as he wrote down.

avatar image meat5000 ♦ · Nov 05, 2013 at 07:40 PM 0
Share

I'm betting, yes. As his second script is clearly having trouble accessing his 'static' variable.

Also, I don't see the word float anywhere.

avatar image ozturkcompany · Nov 05, 2013 at 07:58 PM 0
Share

Oh i knew that as ive learned from the tutorials. As long as you dont specify what it is like is it a float or int? So var number = 5; is a float number. We didn't define that it is float but unity will understand any number as float if we dont specify that it is a integer Anyway this isnt the answer of the question or anything though.

avatar image meat5000 ♦ · Nov 06, 2013 at 11:05 AM 0
Share

Aye, an integer in JS is actually a parsed float. But I imagine with a static variable it'd be important to declare type. If it is accessed by multiple elements this seems important.

avatar image Michael_glu · Nov 06, 2013 at 07:13 PM 0
Share

I tried int doesn't help

avatar image
0

Answer by Azrapse · Nov 08, 2013 at 10:26 AM

You have two scripts.

That makes two different classes.

You declare score as static in the first class. That means that score becomes "kind of" global for all objects of that class.

However, you try to access it from the other class. That other class cannot access it just by simply using its name, because score belongs to another class.

To access it from the bullet class, and supposing that your first script is called ScoringSystem.js, replace the line that is giving you the error message in the bullet class with this:

 ScoringSystem.score = ScoringSystem.score + 100;

However, beware. This allows just for one score in the game. You cannot have several simultaneous players with their own scores. So keep that in mind.

Comment
Add comment · Show 3 · 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 fafase · Nov 10, 2013 at 08:45 AM 0
Share

Don't bother, I gave that answer 5 days ago he did not bother trying. He does not want information, he wants solution so just copy paste his script with the correction if you hope for some recognition.

avatar image Michael_glu · Nov 11, 2013 at 06:41 PM 0
Share

look, you did not understand, I tried all of them the instance you posted the comment, but i just ran into more trouble. I am a high school student, not a professional,and i have been trying to solve this problem for 2 weeks now.

avatar image Michael_glu · Nov 11, 2013 at 06:44 PM 0
Share

thank you all, I finally fixed it

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

20 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

Related Questions

Access components in ALL children 1 Answer

Water4 reveals all underwater objects when Global Fog is applied to the camera 0 Answers

Go Idle after Animation 0 Answers

Activating and deactivating problem 2 Answers

Possible Fall Damage OnCollisionEnter Won't Work! 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