x


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!

more ▼

asked Dec 11 '10 at 05:49 AM

Southpaw444 gravatar image

Southpaw444
11 2 2 3

I don't know why my code sample is all broken up like that, sorry I'm a code newb.

Dec 11 '10 at 06:06 AM Southpaw444
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Use transform.Find instead:

http://unity3d.com/support/documentation/ScriptReference/Transform.Find.html

It takes a path relative to the current object

more ▼

answered Dec 11 '10 at 06:02 AM

Mike 3 gravatar image

Mike 3
30.5k 10 65 253

Thank you very much!!

Dec 11 '10 at 06:07 AM Southpaw444
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1683
x655
x255

asked: Dec 11 '10 at 05:49 AM

Seen: 1000 times

Last Updated: Dec 11 '10 at 05:49 AM