Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by caprariualex · Jan 08, 2020 at 12:49 PM · 2d-platformershootingenemy ai

How can i do? (enemy don't refind player game object)

I don't know how to do. My enemy stop shooting player and after death(player) i implemented a respawn script, but enemy don't "refind" the player. How can i do? My code:

 public GameObject bulletPrefab;

 public float fireRate = 0;

 private GameObject pl;

 float timeToFire = 0;
 Transform firePoint;

 void Awake()
 {
     firePoint = transform.Find("FirePoint");
     if (firePoint == null)
     {
         Debug.LogError("No firePoint? WHAT?! ENEMY");
     }
 }

 void Update()
 {
     if (Time.time > timeToFire && GameObject.Find("Player") != null)
     {
         CheckIfTimeToFire();
     }
 }

 void CheckIfTimeToFire()
 {
     timeToFire = Time.time + 1 / fireRate;
     Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
 }
Comment
Add comment
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

2 Replies

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

Answer by logicandchaos · Jan 08, 2020 at 03:48 PM

Using a tag on the prefab and using find by tag should solve your problem. However as IINovaII said doing so in update is inefficient. You should make a reference to the player: GameObject player; GameObject playerPrefab; then player you can instantiated your player like this: player = Instantiate(playerPrefab); Then you don't have to use any find calls at all. Instantiating is also inefficient tho, so what you might want to do is instead of destroy and instantiate you use player.gameObject.SetActive(false); and player.gameObject.SetActive(true); Then you could declare your player as public and just assign it in the editor in you want, or instantiate it code in Start(). If you use player.gameObject.SetActive(false); and player.gameObject.SetActive(true); You can use OnEnable() and OnDisable() methods to do things when the player is activated or deactivated. You want to use as little update loops in your projects as well. I like having just one.

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 caprariualex · Jan 08, 2020 at 05:06 PM 0
Share

Simple..add a tag and try to search it. Thx man. :DDD

avatar image
0

Answer by IINovaII · Jan 08, 2020 at 02:54 PM

The possible reason why the enemy doesn't find the player is if the newly created player object isn't named as Player anymore. You'll have to confirm that part yourself if that's the case.

On a side note, try to avoid using GameObject.Find within Update() since it can yield you unnecessary performance loss. Instead, when player is re spawned, you can search for all the enemies and cache the player reference within the enemy script. But this just one possible ways out of many.

GameObject.Find Link

Comment
Add comment · 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

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

131 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

2d enemy ai 0 Answers

Avoid enemy sprite overlapping 1 Answer

3 Directional Shooting in 2D Platformer 0 Answers

Shooting a bullet with the same rotation as the gameobject that spawns it. 2D 1 Answer

RayCast2D CompareTag returning error 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