Items spawn and will not stop spawning.
I have a spawn point that spawns a collectable. Once its collected, I need another item to spawn after so many seconds. The code works except for the fact that they spawn and continue to spawn until the game breaks. I have looked at check sphere and the like, but cant get it to work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinSpawner : MonoBehaviour
{
public GameObject Coin;
public bool stopspawning = false;
public float spawnTime;
public float spawnDelay;
public float sphereRadius;
// Start is called before the first frame update
public void Start()
{
InvokeRepeating("SpawnObject", spawnTime, spawnDelay);
}
public void SpawnObject()
{
Instantiate(Coin, transform.position, transform.rotation);
if (stopspawning)
{
CancelInvoke("SpawnObject");
}
}
public void NoSpawn()
{
// stop the spawning of item
if (Physics.CheckSphere(transform.position, sphereRadius))
{
CancelInvoke("SpawnObject");
}
}
}
Answer by Phorj · Jan 06 at 01:18 AM
A few things from what i can see:
You call InvokeRepeating() in Start() but don’t call NoSpawn() ever. NoSpawn() will need to be called by Update() (which you don’t have so there is no way to let your code know what is happening per frame).
CancelInvoke() is not called from NoSpawn() for this reason
You also don’t call CancelInvoke() from SpawnObject(), because the bool stopspawning is set as false, and using an if statement in shorthand like
if (stopspawning) means you’re saying if (stopspawning == true), which means it will never call.
So here's a few adjustments:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinSpawner : MonoBehaviour
{
public GameObject Coin;
public bool stopspawning = false;
public float spawnTime;
public float spawnDelay;
public float sphereRadius;
// Start is called before the first frame update
public void Start()
{
InvokeRepeating("SpawnObject", spawnTime, spawnDelay);
}
// Added Update() so it checks the transform each frame through the NoSpawn() method
public void Update()
{
NoSpawn();
}
public void SpawnObject()
{
Instantiate(Coin, transform.position, transform.rotation);
if (stopspawning)
{
CancelObjectSpawn();
}
}
public void NoSpawn()
{
// stop the spawning of item
if (Physics.CheckSphere(transform.position, sphereRadius))
{
stopspawning = true;
}
}
// Added a separate method to cancel the spawning of the object
// This method is called if stopspawning is true, which in turn is checked each frame
// in Update() via the NoSpawn() method
public void CancelObjectSpawn()
{
CancelInvoke("SpawnObject");
}
}
Thank you, I will work this in to my code. If I get things working right Ill mark it as the answer. I appreciate your time and effort.
I have worked this into the code, the items never respawn. I have adjusted the size of the check sphere. Once it is collected, the item never respawns.
Ahh yes my bad. I got carried away and then was like "why do you want to keep respawning it" forgetting the original question.
I also didn't want to modify your code too much, with the intent of giving you a nudge in the right direction, but here we are.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinSpawner : MonoBehaviour
{
public GameObject Coin;
public float spawnTime;
public float spawnDelay;
public float sphereRadius;
public float coinCount = 0;
// Start is called before the first frame update
public void Start()
{
// Don't really need it at Start() since the Update() function will do it
// But put it here in case you have a specific reason to have it immediately
// SpawnObject();
}
public void Update()
{
SpawnObject();
NoSpawn();
}
public void SpawnObject()
{
// First time around coinCount will be 0 so it will RespawnObject()
if (coinCount <= 0)
{
InvokeRepeating("RespawnObject", spawnTime, spawnDelay);
}
}
public void NoSpawn()
{
// Stop the spawning of item
// Minus 1 from coinCount
if (Physics.CheckSphere(transform.position, sphereRadius))
{
CancelInvoke("RespawnObject");
coinCount -= 1;
}
}
public void RespawnObject()
{
// Spawns the Coin
// Adds 1 to coinCount
Instantiate(Coin, transform.position, transform.rotation);
coinCount += 1;
}
}
Answer by UTPBSTEM · Jan 11 at 02:03 PM
I've been working with this for a few days now, The code works except for when the item respawns, it spawns 11. @Phorj
Sounds like you need to reset the coinCount....
Based on these questions, I'm guessing you don't have much experience with C#?
As I've said previously, I'm more interested in nudging you in the direction needed instead of doing everything for you. I suggest having a look through Youtube as there are plenty of tutorials that cover this exact topic on how to despawn / respawn items.
I personally would not use InvokeRepeating() but left it as that's what you were using - there are multiple ways to do this, it really comes down to the way you've structured your game and what tickles your fancy!