Using spawners script variable to set relevant speed of an Enemy... Im bewildered
Hey,
I'm having difficulty setting a range of enemy speed upon Instantiate from Spawner GameObjects ( I have 8 of them). Briefly I'm working on a game where enemies are coming from 4 different directions with the purpose of fighting them off.
I have 2 scripts; EnemyBehaviour.cs, where I execute movement speed, Collision detection and destroy upon collision. EnemySpawnPoint.cs, where I Instantiate EnemyObjects from object's transform and move it towards certain GameObject within the game. I also intend to set the speed of instantiated objects.
The struggle I faced is when I am trying to call .getSpeed() function in EnemyBehaviour from my Spawner Function, it sets the same speed for all of THEM despite I have different values in the inspector and what I want is to set different speeds of each of those SpawnGameObjects so when the enemy spawns it has relative speed to the object's variable.
NOTE: I have removed unnecessary code
All answers will be appreciated and I hope it isn't a big chunk of "text" :)
EnemyBehaviour.cs
public float health = 500;
public GameObject projectile;
public GameObject grave;
public float projectileSpeed = 10f;
public float fireRate = 0.5f;
public float speed;
private GameObject spawnerObject;
Start()
void Start()
{
spawnerObject = GameObject.FindGameObjectWithTag("SpawnPoint");
speed = spawnerObject.GetComponent<EnemySpawnPoint>().GetSpeed();
}
Move()
void Move()
{
//Vector2 direction = (transform.position - grave.transform.position).normalized;
//rigidbody2D.velocity = direction * (speed * Time.deltaTime);
transform.position = Vector3.MoveTowards(transform.position, grave.transform.position, Time.deltaTime * speed );
}
EnemySpawnPoint.cs
public GameObject[] Enemies;
public float DelayTime = 2f;
public float TotalEnemies = 5f;
public float enemySpeed;
private float nextSpwan;
private float totalSpawn;
void Spawner()
{
if(totalSpawn >= TotalEnemies)
{
Debug.Log("Can't spawn anymore");
return;
}
if(Time.time > nextSpwan)
{
nextSpwan = Time.time + DelayTime;
Instantiate(Enemies[Random.Range(0, Enemies.Length)], transform.position, Quaternion.identity);
Debug.Log ("Spawned");
totalSpawn++;
}
}
public float GetSpeed()
{
return enemySpeed;
}
}