Greetings, as it stands I’m looking for a bit of help.
I’m trying to attach a “Spawner” to an object, and get an object to respawn after being destroyed. I have code that SHOULD check for it and respawn it. But alas, it does not. Spawning the object in works fine, and the code runs all the way through without error. But when the object is destroyed, it no longer runs the code for some strange reason.
I’m sure I’m missing something simple, some small piece of code, or a method type.
If anyone has any suggestions, I’ve attached the code here.
Thank you kindly.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnerObject : MonoBehaviour {
public GameObject redGuy;
public GameObject greenGuy;
public GameObject redGuyLeft;
public GameObject greenGuyLeft;
int greenSpawned = 0;
int redSpawned = 0;
float numberGen;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
numberGen = Random.Range(1,5);
print (numberGen);
spawnRed ();
}
void spawnRed ()
{
if (numberGen == 1) {
if (GameObject.Find ("ARMYgirlPref") == null) {
if (redSpawned == 0) {
Instantiate (redGuy, new Vector2 (272.27f, 285.17f), Quaternion.identity);
redSpawned = 1;
RedRespawn ();
}
}
}
}
void RedRespawn () {
if (GameObject.Find ("ARMYgirlPref") != null) {
redSpawned = 0;
spawnRed ();
}
}
}
Don’t use GameObject.Find. Keep track of objects you spawn in when you create them.
Also note that your numberGen variable is a float, but you want it to equal 1. It may be pretty rare that it equals exactly 1. You may want numberGen to be an int instead of a float, which may be where your problem is.
Your code doesn’t make much sense. Why don’t you just store a reference to this instantiated object instead of running these GameObject.Find calls all the time? Why are you setting redSpawned to 1 just to set it back to 0 when you call RedSpawn() on the next line?
I’m not really sure what you’re trying to do with the numbergen. It is generally not a good idea to be checking if a float is exactly a specific integer value. If you’re just trying to add some randomness to respawns, I’d suggest just generating a random timer that you count down.
You could do something like below. You could also simplify the logic with coroutines, but since you’re using GameObject.Find (which you should never use, especially from Update of all places) I’m going to make the assumption you’re fairly new so I’ll leave those out of the picture.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnerObject : MonoBehaviour {
public GameObject RedGuyPrefab;
private GameObject redGuyObject;
private float timeUntilAllowedToSpawn = 0f;
private bool spawned = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (redGuyObject == null)
{
if (spawned == true) //means redGuyObject just died, so lets set another random spawn timer
{
spawned = false;
timeUntilAllowedToSpawn = Random.Range(3f, 30f); //set a random timer for a new spawn between 3 and 30 seconds
}
else
{
timeUntilAllowedToSpawn = timeUntilAllowedToSpawn - Time.deltaTime;
if (timeUntilAllowedToSpawn <= 0f)
{
spawnRed ();
}
}
}
}
void spawnRed ()
{
redGuyObject = (GameObject)Instantiate(RedGuyPrefab, new Vector2 (272.27f, 285.17f), Quaternion.identity);
spawned = true;
}
}
Your script is almost right but if he uses this than it will never call because your not stating if spawnedredguy is true or not… There is also no reference in this script stating whether or not to destroy the current spawned red guy amd the instantiate another.
If you’re responding to me, I don’t have a variable called “spawnedredguy”.
“spawned” is set to false from the get go, so since redGuyObject would begin as null, spawned is false, and timeUntilAllowedToSpawn is 0f, the redGuyPrefab would be instantiated on the first execution of Update. Furthermore I don’t check whether to destroy the existing object and instantiate another because I wrote it to not even try to instantiate another until the first one is already destroyed, since that is what it appeared the original poster was trying to do. I’m assuming the OP has some type of gun script that destroys redguys so doesn’t need help on the destroy part.
When redGuyObject is destroyed, redGuyObject again evaluates to null, which then kicks off another random timer to instantiate another one.
The random number generator is set to Float, because for some reason Unity throws an error if I set it to INT.
The random number generator is supposed to pick a number and depending on that number is supposed to spawn a different GameObject/NPC.
This is for the main menu of my game, the NPC will then walk to the other-side of the screen and then be deleted by another GameObject I have set up, at which time the random number generator causes a different (Or same) NPC to spawn.
Also the code piece that sets redSpawned = 1 or 0, is because if this is not set, the game continues to make clones of my NPCs non stop.
That is where the problem lies, it does not spawn them again after being destroyed because of this, It just stops.
@Joe-Censored Yes, I am sadly pretty new at scripting, thank you for the help, I’ll read through your code and see if I can’t get this up and running now.