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 Dyson6 · Mar 07, 2011 at 12:28 AM · staticoncollisionentercountervar

Make Score Go Up At a Collision

Okay I want it when a block collides with water, the player's score counter goes up by 1

I have a Gui Text object to start the Counter which just has

static var Counter = 0;

And on the water object I have a script to make the counter go up by 1 when it collides with a box that says

function OnCollisionEnter (other : Collision) {
    if (other.gameObject.name == "smallcube")
    Counter+=1;
}

But I'm still getting an error that says Unknown Identifier : 'Counter' Can anyone tell me what's going wrong here?

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
1

Answer by Joshua · Mar 07, 2011 at 12:33 AM

To access a variable that you have made static you first have to declare in the script the variable is from. So it would be NameOfTheScriptCounterIsIn.Counter+=1;

To make it more clear, if the Counter is in a script named ScoreCounter it would be:

ScoreCounter.Counter+=1;

By the way, it's good to get into the habit of not capitalising the first letter of a variable.

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 Statement · Mar 07, 2011 at 02:12 AM 0
Share

Could you please tell me why it's good to get into the habit of doing so? Personally I dislike the approach Unity has, although I can see some reasons to why (you'd not easily access static members). .NET coding guidelines suggest that all public members are pascal cased (starting with captial letter).

avatar image Statement · Mar 07, 2011 at 02:14 AM 0
Share

The reason I am following with unitys convention is that the entire framework seems to follow this, and I want to stay consistent with the rest of the community. If you look at any .net framework class you'll see they are all capitalized.

avatar image Statement · Mar 07, 2011 at 02:16 AM 0
Share

Funnily enough I was writing my own engine in Xna, and I wanted to try on some of the architecture found within Unity. Having a member with the same name as the type becomes a hassle, if you then want to access static members. For example, having a member called Physics that derive from Physics won't let you to access static members by Physics.SomeStatic$$anonymous$$ember.

avatar image Dyson6 · Mar 07, 2011 at 02:24 AM 0
Share

Still isn't working for me. I got the error to go away but the zero never goes up when a box collides with the water

avatar image Joshua · Mar 07, 2011 at 12:33 PM 1
Share

Statement, I said it would be a good thing because he seems like hes new to scripting, and it's good to force yourself to get into the habit of following the coding guidelines then. Once you're comfortable with it, like you obviously are, you can do whatever you want - but in the beginning it's practical if your code looks like the examples you see around you.

avatar image
0

Answer by Tyler Starr · Mar 07, 2011 at 05:27 AM

I use this script:

var score = 0;

 var scoreText = "Score: 0";
 var mySkin : GUISkin;

 function OnTriggerEnter( other : Collider ) {
     Debug.Log("OnTriggerEnter() was called");
     if (other.tag == "Asteroid") {
         Debug.Log("Other object is a coin");
         score += 25;
         scoreText = "Damage%: " + score;
         Debug.Log("Score is now " + score);
         Destroy(other.gameObject);
         GameObject.Find("Main Camera").animation.Play();
     }
 }

 function OnGUI () {
     GUI.skin = mySkin;
     GUI.Label (Rect (100, 10, 500, 200), scoreText.ToString()); }

     function Update(){
         if( score == 100){
             Application.LoadLevel(0);
         }
     }

it adds score to GUI skin when you collide with an object tagged Asteroid. Then when your score is 100 it returns to Level0.

-Hope this script will satisfy for your needs.

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

How to use a static var 2 Answers

Static variables explanation. 2 Answers

guitexture a static var? 2 Answers

set SmoothFollow target at runtime via static var 3 Answers

Increase Static Var gradually 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