Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by DirtyBong · May 28, 2020 at 09:12 PM · unity 5programmingintincreaseincrement

Int wont increase

Hi there, I'm a new programmer and I have a question about my code! I want to set it so that my bool goes from true to false whenever my enemies hit a certain amount that have been initiated. The problem is my int of enemyCount is just not incrementing. Any Idea why? P.S don't be afraid to criticize anything else about my code as I'm still new and want to improve.

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemyDeath : MonoBehaviour
 {
     [SerializeField] private GameObject player;
     [SerializeField] private GameObject deathTrigger;
     [SerializeField] private ParticleSystem enemyGhostDeathFX;
 
     [SerializeField] private float deathDelay;
     [SerializeField] private int enemyLimit;
 
     private int _enemyCount;
 
     private bool _giveCoin;
 
     //TODO Stop player from giving coin and spawning enemies when the limit of enemies is hit
     void Start()
     {
         _enemyCount = 0;
         _giveCoin = true;
         player = GameObject.Find("Player");
     }
 
     private void Update()
     {
         if (_enemyCount > enemyLimit)
         {
             _giveCoin = false;
         }
     }
 
     private void OnTriggerEnter(Collider other)
     {
         if(other.CompareTag("Player"))
         {
             deathTrigger.SetActive(false);
             EnemyDeathSequence();
         }
     }
 
     private void AddEnemyCoin()
     {
         player.GetComponent<EnemyCoin>().enemyCoinAdd();
     }
 
     private void EnemyDeathSequence()
     {
         enemyGhostDeathFX.Play();
 
         Destroy(gameObject, deathDelay);
         EnemyRespawn();
 
         RingAbilityAdd();
 
         print(_giveCoin);
         
         CheckToGiveCoin();
     }
 
     private void CheckToGiveCoin()
     {
         if (_giveCoin == true)
         {
             AddEnemyCoin();
         }
         else if (_giveCoin == false)
         {
         }
     }
 
     public void EnemyRespawn()
     {
         _enemyCount++;
         Instantiate(PrefabManager.Instance.EnemyPrefab);
         print(_enemyCount);
     }
 
     private void RingAbilityAdd()
     {
         player.GetComponent<Ability>().AddRingAbility();
     }
 }
 
Comment
Add comment · Show 2
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 razivo · May 31, 2020 at 07:05 AM 0
Share

you might want to initialize the _enemycount int to 0 (or what is appropriate).
you might want to add a debug statement before the "_enemyCount += 1" to check if it's a problem with that part of the code.
if not, just propagate outwards with debug statements until you get to the problematic line.
also, could it be that the initiated components change this value?

avatar image DirtyBong razivo · Jul 27, 2020 at 05:20 PM 0
Share

That's actually not a bad debugging method for this issue. I actually ended up solving the issue with another users idea but Ill keep your idea in $$anonymous$$d next time I run into a similar bug.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by tadadosi · May 31, 2020 at 06:37 PM

With a quick look it seems like you are destroying the gameobject attached to EnemyDeath who is also the one that should increase _enemyCount, after you destroy it, that count is gone and it will just start from zero. Maybe you should think about adding a score manager with a static count for your enemies or a script that manages your enemies and keeps the count without being destroyed.

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 DirtyBong · May 31, 2020 at 07:26 PM 0
Share

This fixed my problem. This script was meant to just act as EnemyDeath but ins$$anonymous$$d starting acting like a manager. I added a new GameObject in my level and applied a EnemyCount$$anonymous$$anager to keep track of the amount enemies In my level like I needed to.

avatar image
1

Answer by unity_ltNnNYF1WDuhbA · May 31, 2020 at 11:28 AM

I think the problem is that your EnemyDeath function is not called and it happens because you are not checking who is Enter on Trigger: You should specify who is on Trigger Enter. Give a tag to your player and then check it like: if(other.tag == "tagthatyougivetoplayer") the do stuff.

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 unity_ltNnNYF1WDuhbA · May 31, 2020 at 11:29 AM 0
Share

in your case it's not other.tag, but collide.tag i guess, anyway, better search for it =))

avatar image DirtyBong · May 31, 2020 at 06:23 PM 0
Share

Didn't fix the problem, however you are right that I need to specify who's on the trigger enter. I went ahead and changed that.

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

221 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 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

Dedicated server for unity game 0 Answers

How can I have a life added bar every time I reach 5000 points 0 Answers

Map creator inside my game 0 Answers

The location of the object by points 0 Answers

Randomly set an integer as positive or negative? 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