- Home /
How can i spawn and Despawn for a specific time using this script?
Hi I want to spawn a prefab for 10 seconds before destroying it and respawning it again. If player captures it than i want to immediately spawn a new one.
I tried putting script on the prefab collider of object so that when it collides with the player it immediately spawns a new one but that doesnt work.
public GameObject point;
private Vector2 screenBounds;
public float respawnTime = 10f;
void Start()
{
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
spawnStar();
StartCoroutine(starWave());
}
public void spawnStar()
{
Debug.Log("Yeah it works");
GameObject star;
star = Instantiate(point) as GameObject;
star.transform.position = new Vector2(Random.Range(-screenBounds.x, screenBounds.x)*0.3f, Random.Range(-screenBounds.y, screenBounds.y) * 0.3f);
}
IEnumerator starWave()
{
while (true)
{
yield return new WaitForSeconds(respawnTime);
spawnStar();
Destroy(point);
}
}
Answer by singhlaxman9761 · Apr 09, 2021 at 05:04 AM
This is main script where first prefab will instantiate
public class Test : MonoBehaviour
{
public GameObject prefab;
private Vector2 screenBounds;
public float respawnTime = 10f;
void Start(){
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
spawnStar();
spawnStar();
}
public void spawnStar()
{
Debug.Log("First Init");
GameObject star;
star = Instantiate(prefab) as GameObject;
star.transform.position = new Vector2(Random.Range(-screenBounds.x, screenBounds.x) * 0.3f,
Random.Range(-screenBounds.y, screenBounds.y) * 0.3f);
}
}
Attech PrefabTest.cs on prefab and assign prefab it
public class PrefabTest : MonoBehaviour
{
public GameObject prefab;
private Vector2 screenBounds;
public float respawnTime = 10f;
void Start()
{
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
Invoke("spawnStar", respawnTime);
}
public void spawnStar()
{
Debug.Log("Yeah it works");
GameObject star;
star = Instantiate(prefab) as GameObject;
star.transform.position = new Vector2(Random.Range(-screenBounds.x, screenBounds.x) * 0.3f,
Random.Range(-screenBounds.y, screenBounds.y) * 0.3f);
DestroyImmediate(gameObject);
}
}
Answer by Rapizer · Apr 09, 2021 at 05:07 AM
I'm not sure what you mean, the script above only spawns point every 10 seconds, nothing in the script tells it to spawn a new star on collision with the player. Also, which object are you putting the above script in exactly?
Also, I think the reason the above script isn't working is because you shouldn't be destroying "point", you should be destroying "star". (untested code so may not work, if it doesn't, tell me the error message it's getting)
private Vector2 screenBounds;
public float respawnTime = 10f;
private GameObject star;
void Start()
{
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
spawnStar();
StartCoroutine(starWave());
}
public void spawnStar()
{
Debug.Log("Yeah it works");
star = Instantiate(point) as GameObject;
star.transform.position = new Vector2(Random.Range(-screenBounds.x, screenBounds.x)*0.3f, Random.Range(-screenBounds.y, screenBounds.y) * 0.3f);
}
IEnumerator starWave()
{
while (true)
{
yield return new WaitForSeconds(respawnTime);
Destroy(star); // destroy star not point
spawnStar();
}
}
This works but how can i spawn it immediately after player collides with it?
Your answer
Follow this Question
Related Questions
Cannot change the value of this integer that I use for array,Cannot change the value of this one. 0 Answers
Mouse Click Walk to Idle Animations 0 Answers
Unity Admob didint show in android device 0 Answers
AI script that follows player in a distance 1 Answer
Cannot create mesh from code,Cannot use Script to Generate 2d mesh 0 Answers