- Home /
GameObject.Find - Need help...
I've been working on a top down shooter, and I've made a simple enemy that follows the player and shoots projectiles. My problem is in my script, where I instantiate the projectile prefab. The script works just fine for one enemy, but once there is more than one enemy, the script conflicts with itself. (Note that each enemy has this script attached to it). The problem comes from the gameObject.find. The projectiles dont instantiate on their respective points of origin. If someone could enlighten me on how to get this script to only talk to itself, instead of each other enemy I would greatly appreciate it. I was hoping for something like...
gameObject.find("/this.EnemyBulletSpawn") to refer to only the projectile spawn attached to the current object and not all the other enemies.
Here is the code...
var bulletPrefab: Transform;
var bulletLive:boolean = false; var shotTimer = 1.5;
function Update () { shoot(); }
function shoot(){
if(bulletLive == false){
var bullet = Instantiate(bulletPrefab, GameObject.Find ("enemyBulletSpawn"). transform.position, Quaternion.identity);
bullet.rigidbody.AddForce(transform.forward * 2000);
bulletLive = true;
shotCoolDown();
}
}
function shotCoolDown(){ yield WaitForSeconds(shotTimer); bulletLive = false; }
I'm sorry if this is messy, I'm new to the forum and if anyone could throw me a hint on code formatting, that would be appreciated too! Thanks in advanced!
I don't know why my code sample is all broken up like that, sorry I'm a code newb.
Answer by Mike 3 · Dec 11, 2010 at 06:02 AM
Use transform.Find instead:
http://unity3d.com/support/documentation/ScriptReference/Transform.Find.html
It takes a path relative to the current object
Your answer

Follow this Question
Related Questions
Instantiating in a position relative to an object. 1 Answer
find the exact point on which a projectile will hit enemy 3 Answers
Giving Instantiated Object Velocity Causes Wrong Position 1 Answer
How to create detonation time for grenade projectile. 0 Answers
How can I get a GameObject's transform in game and set that to a variable? 1 Answer