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 /
avatar image
-1
Question by MOLB · Feb 03, 2012 at 08:39 PM · aienemyspawningattack

Enemy behavior is not working after the enemy has spawned.

Hi

When I spawn my enemies using Instantiate function, they work as "normal". They are attacking, but not me. They are attacking the point where my player spawned. I do not have any GameObject that is there (that is related to the player). They work as usual otherways. I setted the Prefab up, and everything in it could work. And the original enemy works normal (if that is helpful for any reason). How can I fix this?

Comment
Add comment · Show 2
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 LukaKotar · Feb 03, 2012 at 08:57 PM 0
Share

Is the target set to the same GameObject as the original enemy?

avatar image MOLB · Feb 03, 2012 at 08:58 PM 0
Share

Yes. It is set to the player Prefab. And the player I am using is that Prefab.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Berenger · Feb 03, 2012 at 09:00 PM

It's going to be hard to fix without more information about the way you're doing your AI, but I can guess : When the enemy spawn, does he get a reference to Transform of player, or just a Vector3 ? Because the second one won't be updated over time.

[Edit after reading your comment] You are confusing your player's prefab, which is a file on your computer, and it's instantiation, (is that a word ?) which is some memory space depending on the scene, based on that prefab file. So when you move your player, the prefab remain the same. Conclusion, you must reference the instantiated player, not the prefab one.

Comment
Add comment · Show 4 · 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 MOLB · Feb 03, 2012 at 09:05 PM 0
Share

First I have a Transfrom variable. Then one Transfrom inside the Update function. I use Quaternion.Slerp, so it says that it could rotate towards the target. And when the player comes close enough. The target equals the player (target = player;).

avatar image MOLB · Feb 03, 2012 at 09:33 PM 0
Share

Well, I had it like this before. And it worked... But you mean that I have to put so the target is a "player = player.transfrom" or "player = GameObject.Find("Player")". But that will cause an error. Because the player is a Transform. But it will check after the active player, not the player Prefab. Or is there a better way?

avatar image Berenger · Feb 03, 2012 at 10:33 PM 0
Share

Funny thing about components, if you have Transform player; and if player != null, player.transform won't give an error, but player.transform.transform.transform.transform.transform won't either, it's just going to perform the GetComponent several times.

With that said : You need to find the player's game object, then acces it's Transform. Here is some code, to get rid of all that name confusion :

GameObject playerGameObject = FindObjectWithTag( "Player" ); Transform playerTransform = playerGameObject.transform;

Note that I'm not used to javascript, you might have some mistakes. Find an object with tag is much faster than by name, so make sure your player game object has the right tag. And don't do this in Update, but in Start/Awake.

avatar image MOLB · Feb 04, 2012 at 08:05 AM 0
Share

Thanks! It works perfectly!

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Creating basic enemy animation and attacking 4 Answers

Enemy AI help. 0 Answers

enemy ai going to player 2 Answers

How to make a Tron Light cycle Enemy AI Script ? 0 Answers

how can i set target(gameobject) from string(name) 1 Answer


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