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