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
0
Question by SalDaDoor · May 25, 2021 at 05:30 PM · prefabaipathfindingtagsspawning-enemies

Pathfinding AI with Aron Granberg, Prefabs with AI not Retaining Target Transform, Trying to change for AI to Follow GameObjects with Tag "Player" PLZ HELP

I'm making a top down shooter where zombies have to follow the player and waves of zombies come through the windows similar to COD:zombies. Whenever I have the prefab inspector tool open in unity however it doesn't let me set the player as a target. It only allows me to do so after the zombies have spawned in. I think setting the AI to follow a tag would fix it but I'm fairly new to coding. Any help?

 //My Code That Doesn't Work
 using UnityEngine;
 using System.Collections;
 
 namespace Pathfinding {
     /// <summary>
     /// Sets the destination of an AI to the position of a specified object.
     /// This component should be attached to a GameObject together with a movement script such as AIPath, RichAI or AILerp.
     /// This component will then make the AI move towards the <see cref="target"/> set on this component.
     ///
     /// See: <see cref="Pathfinding.IAstarAI.destination"/>
     ///
     /// [Open online documentation to see images]
     /// </summary>
     [UniqueComponent(tag = "ai.destination")]
     [HelpURL("http://arongranberg.com/astar/docs/class_pathfinding_1_1_a_i_destination_setter.php")]
     public class AIDestinationSetter : VersionedMonoBehaviour {
         /// <summary>The object that the AI should move to</summary>
         public Transform target;
         IAstarAI ai;
 
         void OnEnable () {
             ai = GetComponent<IAstarAI>();
             // Update the destination right before searching for a path as well.
             // This is enough in theory, but this script will also update the destination every
             // frame as the destination is used for debugging and may be used for other things by other
             // scripts as well. So it makes sense that it is up to date every frame.
             if (ai != null) ai.onSearchPath += Update;
         }
 
         void OnDisable () {
             if (ai != null) ai.onSearchPath -= Update;
         }
 
         /// <summary>Updates the AI's destination every frame</summary>
         void Update () 
         {
             player = GameObject.FindWithTag("Player").transform;
             if (player != null && ai != null)
             {
                 ai.destination = player.position;
             }
         }
     }
 }
 
 // The Original
 sing UnityEngine;
 using System.Collections;
 
 namespace Pathfinding {
     /// <summary>
     /// Sets the destination of an AI to the position of a specified object.
     /// This component should be attached to a GameObject together with a movement script such as AIPath, RichAI or AILerp.
     /// This component will then make the AI move towards the <see cref="target"/> set on this component.
     ///
     /// See: <see cref="Pathfinding.IAstarAI.destination"/>
     ///
     /// [Open online documentation to see images]
     /// </summary>
     [UniqueComponent(tag = "ai.destination")]
     [HelpURL("http://arongranberg.com/astar/docs/class_pathfinding_1_1_a_i_destination_setter.php")]
     public class AIDestinationSetter : VersionedMonoBehaviour {
         /// <summary>The object that the AI should move to</summary>
         public Transform target;
         IAstarAI ai;
 
         void OnEnable () {
             ai = GetComponent<IAstarAI>();
             // Update the destination right before searching for a path as well.
             // This is enough in theory, but this script will also update the destination every
             // frame as the destination is used for debugging and may be used for other things by other
             // scripts as well. So it makes sense that it is up to date every frame.
             if (ai != null) ai.onSearchPath += Update;
         }
 
         void OnDisable () {
             if (ai != null) ai.onSearchPath -= Update;
         }
 
         /// <summary>Updates the AI's destination every frame</summary>
         void Update () 
         {
             if (target != null && ai != null)
             {
                 ai.destination = target.position;
             }
         }
     }
 }
 
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Snubber · May 25, 2021 at 06:01 PM

The prefab inspector tool is only used for editing the prefab file which has not been loaded into the scene yet. Because you're editing a zombie that is not loaded into the scene yet there is no way for it to know about the player which exists within the scene.

Your idea to search for player based on the tag should work. Just make sure that the player actually has the tag. You could also use Object.FindObjectsOfType and put in the type that is whatever Component your player has.

If you are new to coding you might not have used the debugger yet. I highly recommend trying it. It allows you to pause the code at a certain point and see what values certain variables have. For example, after the line player = GameObject.FindWithTag("Player").transform you can pause the code and check to make sure that a player is actually found. Each code editor has a different way to attach the debugger: https://docs.unity3d.com/Manual/ManagedCodeDebugging.html

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

230 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 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

Can you bake nav mesh in prefab mode? 0 Answers

How to speed up built-in Unity pathfinding using navMesh? 1 Answer

Navmesh path without agent 1 Answer

Storing Vector3's and Detecting Gameobjects 1 Answer

How to know how far one object are from the corners of another object. 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