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 Vingo3105 · May 05, 2015 at 05:05 AM · scoretrain

Problem whit score system?

Hi, im making a Train of though clone (a luminosity.apk game from the playstore). In my score system, the player gain a point everytime a train collide with a house of the same color. I made every train and house an independnt GameObject, and I atached this piece of code to every house.

 public int puntos;


 // Use this for initialization
 void Start () 
 {
     puntos = 0;    
 }

 void Update () 
 {
 }

 
 void OnTriggerEnter (Collider other)
 {
     if (other.gameObject.name == "TrenRojo" && this.gameObject.name == "CasaRoja")
     {
         other.gameObject.SetActive (false);
         IncrementarPuntos ();
     }
     else
     {
         other.gameObject.SetActive (false);
     }

     if (other.gameObject.name == "TrenRojoO" && this.gameObject.name == "CasaRojaO")
     {
         other.gameObject.SetActive (false);
         IncrementarPuntos ();
     }
     else
     {
         other.gameObject.SetActive (false);
     }
 
     if (other.gameObject.name == "TrenAzul" && this.gameObject.name == "CasaAzul")
     {
         other.gameObject.SetActive (false);
         IncrementarPuntos ();
     }
     else
     {
         other.gameObject.SetActive (false);
     }

     if (other.gameObject.name == "TrenVerde" && this.gameObject.name == "CasaVerde")
     {
         other.gameObject.SetActive (false);
         IncrementarPuntos ();
     }
     else
     {
         other.gameObject.SetActive (false);
     }

     if (other.gameObject.name == "TrenVerdeO" && this.gameObject.name == "CasaVerdeO")
     {
         other.gameObject.SetActive (false);
         IncrementarPuntos ();
     }
     else
     {
         other.gameObject.SetActive (false);
     }

     if (other.gameObject.name == "TrenNaranja" && this.gameObject.name == "CasaNaranja")
     {
         other.gameObject.SetActive (false);
         IncrementarPuntos ();
     }
     else
     {
         other.gameObject.SetActive (false);
     }

     if (other.gameObject.name == "TrenMorado" && this.gameObject.name == "CasaMorada")
     {
         other.gameObject.SetActive (false);
         IncrementarPuntos ();
     }
     else
     {
         other.gameObject.SetActive (false);
     }
 }

 void IncrementarPuntos ()
 {
     puntos = puntos + 1;
     Debug.Log (puntos);
 }

The problem is that every time a new train collide whit a new house, the score return to 0, so the score its always 1. Sorry for my english, and also for my code, im new in unity.

Thanks :)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by DoTA_KAMIKADzE · May 05, 2015 at 05:08 AM

You need to have a single instance of your "puntos" variable. You can do that in few ways, choose one of them:

1) Just make it static:

 public static int puntos;

2) Create a custom script that will hold your "puntos" variable and get the instance reference of that script in each script where you'll need it and increment variable of that custom script.

3) Save/load your score from/to some file (e.g.: .txt) or use something like PlayerPrefs.

P.S. Personally I'd go for the first one.

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 Vingo3105 · May 05, 2015 at 05:23 AM 0
Share

Thank u, iv already tried the 1 and 2, and both work!

avatar image Vingo3105 · May 05, 2015 at 02:12 PM 0
Share

Thank u, i already tried the 1 and 2, and both worked!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

DontDestroyOnLoad Game Score/Lives 1 Answer

Having trouble updating the text in my GUI 2 Answers

How to get screen coordinate of plane 1 Answer

Stop Score From Adding? 1 Answer

I need help for delete old highscore 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