Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
This question was closed Apr 13, 2014 at 10:39 PM by getyour411 for the following reason:

Other

avatar image
0
Question by ArjunTheMiella · Feb 08, 2014 at 05:46 PM · instantiateprefabclone

Instantiate Object Problem

Hi

I'm making a 2d space shooter, very typical controls, WASD to move and mouse to shoot. I began with a set amount of enemies that I placed by hand, but I obviously don't want that, so I'm trying to instantiate them dynamically.

Here's the problem..The enemies are spawned in fine, but their behavior seems to be wrong according to my code. I want them to simply look at the location of the player, then move in that direction. The most stupid AI, but it should work on update.

The enemies that I placed by hand work fine, but only the instantiated clones will not properly work. Instead of tracking the player's position on update, they appear to be only ever moving toward the starting position of the player at the beginning of the game.

In my player class, I instantiate the enemies like:

 public void SpawnEnemies() {
             int x = 0;
             while (x < 10) {
                 Vector3 ppos = transform.position;
                 Vector3 spos = new Vector3(0, 0, 0);
                 spos.Set(ppos.x + x + 5, ppos.y + x, ppos.z);
                 Instantiate (enemy1, spos, transform.rotation);
                 numEnemies++;
                 x++;
             }
         }

And in the enemy class, where I believe the issue lies, they move like this. Note that the variable player is a Transform that is set to the prefab of the player.

 void Update () {
     transform.LookAt (player);
     transform.position += transform.forward * speed * Time.deltaTime;
 }

To reiterate, if I place enemy prefabs in the world by hand, they will behave as intended, but an instantiated clone appears to only ever look at the original starting point of the player. I realize that this is the location in the world that I placed the player prefab, however, I can't see any reason that this doesn't work. Additionally, during runtime I checked to make sure that the clones were being cloned with the proper variables and references via the Inspector, and they are.

Any help is greatly appreciated.

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 ArjunTheMiella · Apr 13, 2014 at 09:53 PM 0
Share

Solved it, I had to update the player field in the script of the enemy after it had been instantiated.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroy 1 prefab object and changing the color/image 1 Answer

(Clone) from Instantiated Prefabs over Network 1 Answer

How to instantiate from prefab, not from instance? 3 Answers

Assign an instantiated GameObject? 1 Answer

Instantiate Clones Itself - Rather than Prefab 0 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