• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Southpaw444 · Dec 11, 2010 at 05:49 AM · instantiateenemyprojectile

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Southpaw444 · Dec 11, 2010 at 06:06 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Southpaw444 · Dec 11, 2010 at 06:07 AM 0
Share

Thank you very much!!

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Instantiating in a position relative to an object. 1 Answer

2d enemy prefab (enemy movement and instantiate) 0 Answers

How can I acces the script of an object that I triggered? 1 Answer

Instantiate with Prefabs 2 Answers

Firing only a single projectile at a time, then adding a second. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges