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 Surprisejedi · May 04, 2015 at 04:03 PM · c#2d-physicsdestroy object

How do you use collision to log number of hits, while duplicates are being destroyed?

 using UnityEngine;
 using System.Collections;
 
 
 public class Brick : MonoBehaviour {
     
     public int maxHits;
     public int howManyHits;
     // Use this for initialization
     void Start () {
     howManyHits = 0;
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     void OnCollisionEnter2D (Collision2D collision) {
         howManyHits = howManyHits + 1;
             print (howManyHits);
         Destroy (gameObject);
         if (howManyHits == 12) {
             Application.LoadLevel ("YOU WON!");
         }
     }
 }

It seems that when it gets destroyed the howManyHits resets. Can anyone help me with this?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by Surprisejedi · May 04, 2015 at 05:09 PM

I need it because when it gets 12 hits it goes on to the next level

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 bartm4n · May 04, 2015 at 05:13 PM 0
Share

Please use comments ins$$anonymous$$d of answers to keep the conversation flow

avatar image
0

Answer by $$anonymous$$ · May 04, 2015 at 11:26 PM

If you want to track how many of this type of gameObject have been destroyed then you need to make howManyHits a static variable.

Try changing this line

 public int howManyHits;

with

 public static int howManyHits;

You may want to refer to this for more info on the static modifier: https://msdn.microsoft.com/en-us/library/98f28cdx.aspx

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
avatar image
0

Answer by bartm4n · May 04, 2015 at 04:46 PM

Destroying the object literally destroys it, so you lose any data associated with it. You would need to store the number of hits on a persistent object, like a game manager or state object of your own design.

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 bartm4n · May 04, 2015 at 05:14 PM 0
Share

I understand that you need it, but if you destroy the gameobject you lose the data attached to it. You will absolutely have to either not destroy the object or maintain the hit count somewhere else on a persistent object.

avatar image
0

Answer by SwitchStance · May 04, 2015 at 11:29 PM

You are deleting the gameobject after it has been hit once. You can delete the gameobject after checking if it has been hit the max number of times.

 if(howManyHits == maxHits)
 {
     Destroy(gameObject);
     Application.LoadLevel("YOU WON!");
 }
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

21 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 avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Transform.position assign attempt not valid, and position is infinity on an object? 1 Answer

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Collider just for Tag (2D) (C#) 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