I have a an empty gameobject in the scene with RandomSpawn script attached to it. And I am spawning fishes in the scene. I have a float speed in RandomSpawn class which is public, and I can use it in the derived class from RandomSpawn. I am increasing the speed by Time.deltaTime in update method inside RandomSpawn. But I cannot see the updated speed while playing. How should I approach for updated speed being applied to every fish object I have created.
Here are my both scripts, first is RandomSpawn and second is Fish derived from RandomSpawn.
public class RandomSpawn : MonoBehaviour
{
public GameObject[] fishPrefabs;
[HideInInspector]public float speed = 3f;
private float minSpawnInterval = 0.5f;
private float maxSpawnInterval = 3f;
private Vector3 spawnPos;
public float zValue;
// Start is called before the first frame update
private void Start()
{
Invoke("Spawn", minSpawnInterval);
}
private void Update()
{
speed += 0.1f * Time.deltaTime;
}
void Spawn()
{
float time = Random.Range(minSpawnInterval, maxSpawnInterval);
Invoke("Spawn", time);
float xPos = 20;
float randZ = Random.Range(-zValue, zValue);
int fishIndex = Random.Range(0, fishPrefabs.Length);
spawnPos = new Vector3(xPos, fishPrefabs[fishIndex].transform.position.y, transform.position.z + randZ);
GameObject fishes = Instantiate(fishPrefabs[fishIndex], spawnPos, fishPrefabs[fishIndex].transform.rotation);
Destroy(fishes, 15f);
}
}
public class Fish : RandomSpawn
{
Rigidbody rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
rb.velocity = new Vector3(-speed, 0, 0);
}
}