Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Pietruu · Jan 06, 2021 at 05:36 PM · unity 2dconsolecounterdestroygameobject

Destroying GameObject does not add to Debug.Log counter

Hi, I'm trying to make a game where a Player picks up presents and delivers it to people

My problem is that that I want to have a counter on my console telling me how many presents the Player has picked, but it's remaining stuck at 1 no matter how many presents I pick. https://i.imgur.com/jDYoQoh.png

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Present : MonoBehaviour
 {
     [SerializeField]
     //setting the amount of time presents need to despawn
     private float despawn = 5f;
 
     public int presentCount = 0;
 
     // Start is called before the first frame update
     void Start()
     {
         //present counter is set to 0
         presentCount = 0;
         //start the despawn timer
         Destroy(this.gameObject, despawn);
     }
 
     // Update is called once per frame
     void Update()
     {
 
     }
     private void OnTriggerEnter(Collider other) 
     {
         //if player picks up present, present is destroyed (still need to add to console counter)
         if(other.transform.name == "Player")
         {
             presentCount++;
             Debug.Log("Present Count: "+ presentCount);
             Destroy(this.gameObject);
         }
     }
 }
 

I know that it has something to do with the gameObject destroying itself therefore not being able to add to the counter, but I'm not sure how to solve this problem. I've tried putting a counter in Start(), I've tried using OnDestroy() but still arrived to the same result.

The result that I want is that every time I pick up a present, Debug.Log adds +1 to the counter.

If you need any other details please tell me and I will add them, 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
Best Answer

Answer by ryanlin138 · Jan 06, 2021 at 06:48 PM

You have to make the presentCount static, otherwise each individual present would have a different count.

Replace Line 11:

  public int presentCount = 0;

With:

  public static int presentCount = 0;

Keep in mind that you would no longer be able to see the parameter in the inspector.

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

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

Related Questions

host set to C:\Program Files\Unity\Hub\Editor\2019.4.9f1\Editor\Data\Tools\RoslynScripts\..\.. 0 Answers

How to add score after destroying object? 1 Answer

Respawn Management 2 Answers

1 exception was raised by workers: See the Console for details. 0 Answers

Unity Console not showing result 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