How do I code a prefab to single out a gameObject of the same type?
Hello!
I am currently in the midst of learning unity and getting into game development. I have learned quite a bit and am quite proud of what I've learned so far. As of right now I have created a for loop.
void SpawnMissles(int misslesToSpawn)
{
if (Input.GetKeyDown(KeyCode.Space))
{
for (int m = 0; m < enemyCount; m++)
Instantiate(misslePrefabs, gameObject.transform.position + focalPoint.transform.forward, misslePrefabs.transform.rotation);
}
}
I have thrown that tidbit in my script that allows the user to control the player. It has worked really well (enemy count was initialized in the update method). In a script that controls the misses behaviors i have a nifty:
void HomingMissles()
{
Vector3 towardsEnemy = (enemyRb.transform.position - transform.position).normalized;
missleRb.AddForce(towardsEnemy * 10, ForceMode.Impulse);
StartCoroutine(MissleFizzOutCountdown());
}
IEnumerator MissleFizzOutCountdown()
{
yield return new WaitForSeconds(2);
Destroy(gameObject);
}
It works almost as expected which I was very happy with I did it without using an help, but I am now completely stumped and can not find the proper resource or question to get my remaining issue resolved. All the missles (however many enemies are remaining) currently spawn and target one specific object. And if I continuing to create more they follow the same one until he is defeated. How would I write lines of code or a line of code to tell each singular missle to focus a singular enemy.
Thank you very much in advance.
You need to assign the enemyRb field in each missile right after they are instantiated. How is enemyRb set currently?
Your answer
