I want each obstacle inside my obstacle list to move backwards in y direction but the obstacles are just spawning and not moving.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemyscript : MonoBehaviour
{
// Start is called before the first frame update
public GameObject[] obstacles;
float smallspawntime;
private float speed = -20f;
public void Start()
{
}
// Update is called once per frame
void Update()
{
if (Time.time > smallspawntime)
{
int obsIndex = Random.Range(0, obstacles.Length);
Vector3 Obstaclepos = new Vector3(Random.Range(-4.08f, 4.4f), -1.345564f, 60);
Instantiate(obstacles[obsIndex], Obstaclepos, Quaternion.Euler(0, 180, 0));
smallspawntime = Time.time + Random.Range(0.2f, 1.3f);
}
Vector3 obstaclemovement = new Vector3(0, 0, speed * Time.deltaTime);
foreach (GameObject obstacle in obstacles)
{
obstacle.transform.position += obstaclemovement;
}
}
}
Is there anyway I can do this without a separate script for each obstacles?