- Home /
Can't figure out how to access an object after creation.,Can't figure out how to access a given instance of the object on mouse click.
Hello, I am spawning waves of enemies and when I click a mouse button I want to damage the given instance of enemy. It only works on the first one that is created, the next one is ignored. Tried finding it by name and by tag but didn't I failed. I just need a hint on how to figure it out.
private IEnumerator SpawnDayWave()
{
Day day = days[dayCount];
for (int i = 0; i < day.count; i++)
{
SpawnEnemy(day.enemy);
yield return new WaitForSeconds(timeBetweenWaves);
}
dayCount++;
}
private void SpawnEnemy(GameObject enemy)
{
spawnPosition = new Vector3(transform.position.x, transform.position.y + 1, Random.Range(-3.3f, 3.3f));
GameObject enemyAlive = Instantiate(enemy, spawnPosition, transform.rotation);
enemyAlive.name = "Enemy1";
enemiesAlive++;
}
Here is my wavespawner and in the start method i start a coroutine.
public class Player : MonoBehaviour
{
public float playerHealth;
private float playerHealthStart = 100;
public Enemy enemyScript;
private float playerDamage = 30;
private Ray ray;
private RaycastHit hit;
public string enemyTag = "Enemy";
private float reloadTime = 2;
private bool isReloaded = true;
// Start is called before the first frame update
private void Start()
{
SetStartHealth();
enemyScript = GameObject.Find("Enemy1").GetComponent<Enemy>();
}
// Update is called once per frame
private void Update()
{
if (playerHealth <= 0)
{
PlayerDied();
}
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (Input.GetMouseButtonDown(0) && isReloaded)
{
Shoot();
StartCoroutine(Reload());
}
}
}
private void Shoot()
{
if (hit.collider.gameObject.CompareTag(enemyTag))
{
Debug.Log("Shooting!");
enemyScript.EnemyHurt(playerDamage);
isReloaded = false;
if (enemyScript.enemyHealth <= 0)
{
enemyScript.EnemyDied();
}
}
else
{
isReloaded = false;
}
}
private IEnumerator Reload()
{
yield return new WaitForSeconds(reloadTime);
isReloaded = true;
}
public void HurtPlayer(float damage)
{
playerHealth -= damage;
}
public void SetStartHealth()
{
playerHealth = playerHealthStart;
}
private void PlayerDied()
{
Debug.Log("GameOver!");
}
}
And my player class where I get my mouse position and check if its the enemy. My idea also was to check for objectswithtag and everytime when I instantiate iterate over a list of enemies and add a new one. Although don't know if it would work or if my performance wouldn't suffer much.
Answer by sacredgeometry · Jan 23, 2020 at 09:28 PM
enemyScript = GameObject.Find("Enemy1").GetComponent<Enemy>();
is grabbing the first enemy with that name that it finds and setting it in the enemyScript
field.
Then you are calling
enemyScript.EnemyHurt(playerDamage);
on it so yes. You are only ever going to attack one enemy and that enemy will be what ever enemy is currently set in that field.
Your answer
Follow this Question
Related Questions
How Expensive is Find function? 3 Answers
How to hit a specific object 2 Answers
What's the most efficient way of finding child objects at runtime? 1 Answer
Instantiating objects to create an infinite level. 1 Answer
find GUIText 1 Answer