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 /
  • Help Room /
avatar image
0
Question by KydDynasty · Aug 23, 2020 at 12:12 AM · if-statementsrespawning

My if is greater then statement won't work.

Hello everyone I am new to coding somewhat picked up unity about 2 years back and just stared to mess around with it few courses later I found myself wanting to pursue making a game. I have been working to get a respawn system so that enemies don't spawn on each other and when they do they add to a respawn counter on another script that's on a different game object in the game, then delete themselves to clean up the game and destroy the enemies that overlapped. I have been able to get it to access the other script to add a counter however now my if statement isn't working to actually respawn my enemy. Here is my code for the spawner. The code inside the fix update is to respawn the enemy and the code in its own method at the bottom is what is being called from another script to add to the int.

 public class RespawnScript : MonoBehaviour
 {
     public int respawnCount = 0;
 
     [SerializeField] GameObject Enemy;
 
 
     // Start is called before the first frame update
     void Start()
     {
         respawnCount = 0;
     }
 
     private void FixedUpdate()
     {
         if (respawnCount > 0)
         {
             Debug.Log("RespawnCount is greater then 0");
             Vector2 ranenemyspawn;
             float enMinX = -2f;
             float enMaxX = 3.5f;
             float enMinY = 7f;
             float enMaxY = 9f;
 
             var ranenemyspawnX = Random.Range(enMinX, enMaxX);
             var ranenemyspawnY = Random.Range(enMinY, enMaxY);
             ranenemyspawn = new Vector2(ranenemyspawnX, ranenemyspawnY);
             Instantiate(Enemy, ranenemyspawn, Quaternion.identity);
             Debug.Log("Enemy Respawned Succes");
             Debug.Log(ranenemyspawn);
             respawnCount -= 1;
             Debug.Log(respawnCount);
             Debug.Log("Enemies Respawned!!!");
         }
     }
 
     // Update is called once per frame
     void Update()
     {
        
     }
 
 
     public void respawnCounter()
     {
         Debug.Log("respawnCounter Iniated");
         respawnCount += 1;
         Debug.Log(respawnCount);
     }
 }

Just incase its needed this is my code on the enemy that is calling the method "respawnCounter()" The top is me declaring my game objects then I go into using them on the bottom. Please ignore the bullet cap and player box that is for something else.

  RespawnScript spawner;
 
     public GameObject spawnerGO;
 
 
     // Start is called before the first frame update
     void Start()
     {
         PlayerBox = Player.GetComponent<BoxCollider2D>();
         BulletCap = Bullet.GetComponent<CapsuleCollider2D>();
         spawner = spawnerGO.GetComponent<RespawnScript>();
     }

 private void OnTriggerStay2D(Collider2D collision)
     {
         Debug.Log("Enemies have spawned ontop of one another");
         if (collision.gameObject.tag == "Enemy")
         {
             Debug.Log("Enemy Respawning");
             spawner.respawnCounter();
             Destroy(this.gameObject);
         }
     }

Any help on this problem would be great have been trying to remake a respawning system to prevent enemies from spawning on-top of another for a few days now and have hit a lot of dead ends. Thank you in advance for any suggestions :)

I have found out this is the problem my respawncount integer is splitting into 2 for some reason. Here is a screen shot of whats going on alt text

this is a screen shot of my debug log. 1-4 on the bottom is where my respawnCount is counting up but for some reason the -1 is all its reading. Even after the 1-4 came up it still was reading -1 on the update check. I made the respawnCount = -1 in the start which is why it starts there however it was still counting -1. Please any help as to why its splitting up my integer would be very much appreciated.

screen-shot-2020-08-24-at-55415-pm.png (243.9 kB)
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
0

Answer by highpockets · Aug 24, 2020 at 10:35 PM

I don’t know what you mean splitting into 2, but I am assuming that what you are seeing is your spawner script has more than one instance. You should only have one instance of that class in the scene. Also I don’t know if you mean to do so, but you are destroying both enemies when they overlap because the trigger will fire on both enemy scripts.


I suggest you reuse these enemies instead of destroying and instantiating, they are both very resource intensive functions. Just reposition one of the enemies that overlap

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 KydDynasty · Aug 25, 2020 at 01:06 AM 0
Share

Hey thanks for the feedback. When I run the game the only script this is on is my "Respawner" gameobject. So I don't know if there can be 2 instances I only have it written once in that one script as well. Also I am aware its destroying both gameobjects I tried to use the check sphere but I wasn't able to get it working so I figure this system could work just as well. I see what your saying and it makes a lot of sense if that's the problem but I dont see anywhere that it could be placed besides that one script. Also my rsepawner is a prefab and I have the prefab linked to the enemy script so I can access the script within my enemy script.

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

207 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

Related Questions

RaycastHit2D ground detection 1 Answer

How do I run an animation then destroy a game object? 1 Answer

How to use or statements in an if statement. 1 Answer

how to make a succession(n+1,5n,etc) that work in if statement? 0 Answers

How to cancel a if statement once touching a collider 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