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 /
This question was closed Feb 01, 2017 at 05:07 AM by Caleb6801 for the following reason:

Other

avatar image
0
Question by Caleb6801 · Apr 19, 2015 at 10:28 PM · collisionscore

Adding score on collision not working

So I have a script that detects collision with a cube that script is

 #pragma strict
 
 var score : int = 1;
 var amount : int = 1;
 var Collide : boolean;
 var currentScore : UnityEngine.UI.Text;
 
 function Start () {
 PlayerPrefs.GetInt("Score");
 }
 
 function OnCollisionEnter(collision : Collision){
      Debug.Log( "Collision! collision.gameObject.name = " + collision.gameObject.name); 
      if(collision.gameObject.name == "Cube"){;
         Destroy(gameObject,0);
         score += 1;
      }
      if(collision.gameObject.name == "Platform"){;
      Debug.Log("Score: " + score);
     Time.timeScale = 0;
      }
   }
 
 function Update () {
 PlayerPrefs.SetInt("Score", score);
 currentScore.text = "" + score;
 }


I want to be able to increase the score by 1 on collision but when it collides the only thing that happens is that it destroys the gameObject and doesn't add any score.

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

  • Sort: 
avatar image
0

Answer by DoTA_KAMIKADzE · Apr 19, 2015 at 10:34 PM

I can't guess what you want to do, but probably one of 2 things:

1) Destroy the cube instead of itself. Then:

 if(collision.gameObject.name == "Cube")
 {
    Destroy(collision.gameObject);
    score += 1;
 }

2) You want to destroy the holder of the script. Then:

 if(collision.gameObject.name == "Cube")
 {
    score += 1;
    Destroy(gameObject);
 }

Also make your score variable static. Note that your code will no longer continue to run after its holder will be removed after the end of all that processing, so you will need to save and set new value to text from some other running script.

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

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Whats wrong with my score script 1 Answer

Most efficient way to get scores 1 Answer

Question about Point adding / Destroying after collision. 0 Answers

Count scripting problem 1 Answer

Score count increase on hit 2 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