How can I modify all gameobjects that have the same script just 1 time?
Hi everyone, im trying to work a little with some spawns. I want for a specific value on the game score to change the time spawn but JUST 1 TIME in each Spawn GameObject. It just modifies 1 GameObject.
Hope u can help me! Thanks!
using UnityEngine;
using System.Collections;
public class EnemyManager : MonoBehaviour
{
public GameObject enemy;
private float spawnTime;
private Transform spawnPoints;
public float time1;
public float time2;
private float timeToSpawn;
void Start (){
spawnPoints = GetComponent<Transform> ();
SetRandomTime ();
timeToSpawn = time1;
}
void FixedUpdate(){
timeToSpawn += Time.deltaTime;
if(timeToSpawn >= spawnTime){
Spawn ();
SetRandomTime ();
}
if(GameController.score > 0 && GameController.score % 2500 == 0){
reduce_time();
GameController.score+=100;
GameController.wave++;
}
}
void Spawn (){
timeToSpawn = 0.0f;
Instantiate (enemy, spawnPoints.position, enemy.transform.rotation);
}
void SetRandomTime(){
spawnTime = Random.Range (time1, time2);
}
void reduce_time(){
if(time1 > 0 && time2 > 5){
time1 -= 0.5f;
time2 -= 0.5f;
}
}
}
Can you repeat the question, it make no sense.
You want a specific score to change an object's spawn time after it has spawned once? Am I reading that right?
//pseudo
if (score == specificScore)
{
if (hasSpawnedAlready == false && spawnTime != newSpawnTime)
{
spawnTime = newSpawnTime;
}
}
Sry I didnt explain it clearly. Yes u're right, but the time doesn't have to change after the first spawn. It has to change in every Spawn(There are 6) when the score % 2500 == 0. So the problem is when the condition of (score % 2500 == 0) is true it reduces to the $$anonymous$$imum value available from the condition. Because of Update() loop.
void reduce_time(){
if(time1 > 0 && time2 > 5){ //<--Here
time1 -= 0.5f;
time2 -= 0.5f;
}
}
So I thought If I modify the score value maybe it would work fine just 1 time per Spawn.
I did this:
if(GameController.score > 0 && GameController.score % 2500 == 0){
reduce_time();
GameController.score+=100; //<-- HERE
GameController.wave++;
}
But it just modify 1 Spawn of 6, and I want to modify all of them, but just 1 time. For example: SpawnTime is (time1=5 and time2=10) I want to reduce both in 0.5f, It would be like (time=4.5f and time2=9.5f) In every spawn.
Thanks for help!