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 MKARSOTNEORS · Nov 19, 2018 at 05:57 AM · getcomponentpathfindingprefab-instance

My question is how to give a newly made prefab a location target.

By this I mean, I have made a prefab of an enemy. I don't know how to tell him where his target is because I can't drag it across to him in the menu screen because he is a prefab and if I create him, I can't make it a get component location either because the enemy doesn't know which one I am talking about. So it comes back to the first problem. I do not know how to get him the target at first. Once he gets the target it works fine. Thanks for to anyone who can fix this issue. I also commented the part where the issue is

using Pathfinding; using UnityEngine; using System.Collections;

[RequireComponent(typeof(Rigidbody2D))] [RequireComponent(typeof(Seeker))] public class EnemyAI : MonoBehaviour { // what to chase public Transform target; // the player // how many times will it check each second to update the path. public float updateRate = 2f; // how often it checks //caching private Seeker seeker; // the finding scirpt private Rigidbody2D rb; // rigidbody 2d. Pretty east

 // The calcualted path
 public Path path;

 // The A.I's speed per second; 
 public float speed = 300f;
 public ForceMode2D fmode;
 public Transform hero; 
 public Transform HomeBase;
 public int currenthealth; 

 [HideInInspector]
 public  bool PathIsEnded = false;

// the max distance from the A.I to a waypoint for it continue to the next way point. public float NextWayPointDistance = 3; // the way point we are currently moving to. private int currentWayPoint = 0;

  void Start()
 {
     currenthealth = GetComponent<Enemy>().health; 
     seeker = GetComponent<Seeker>();
     rb = GetComponent<Rigidbody2D>();
     if (hero == null)
     {
       //  hero.transform.position = 
     }
     if (HomeBase == null)
     {

        // HomeBase.transform.position =
     }

     if(target == null)
     {

         return; 
     }
     // start a new path to the target postion. returning the result to the OnPathComplete method
     seeker.StartPath(transform.position, target.position, OnPathComplete);
     target = HomeBase; 
   StartCoroutine(UpdatePath ()); 
 }

IEnumerator UpdatePath() { if (target == null) {

         yield return false;
     }

     seeker.StartPath(transform.position, target.position, OnPathComplete);
     yield return new WaitForSeconds(1f / updateRate );
     StartCoroutine(UpdatePath());
 }

 IEnumerator UpdateTarget()
 {
     if(target = hero)
     {
         yield return new WaitForSeconds(5f);
         target = HomeBase; 
     }

 }



 public void OnPathComplete (Path p)
 {
    // Debug.Log("We got a path. Did it have an error? " + p.error); 
     if (!p.error)
     {
         path = p;
         currentWayPoint = 0; 
     }
 }

  void FixedUpdate()
 {
     if(GetComponent<Enemy>().health < currenthealth)
     {
         target = hero; 
          
     }
     if (hero == null)
     {
         target = HomeBase; 
     }
     if (target == null)// checks to see if target is null 
     {

         return;
     } 
     if (path == null)// checks to see if target is null
     {
         return; 
     }
     if(currentWayPoint >= path.vectorPath.Count) // tell us if we have reached our target yet
     {
         if (PathIsEnded)
         {
             return;
         }

         
         PathIsEnded = true;
         return; 
     }
     PathIsEnded = false;
     // directions to the next way point
     Vector3 dir = path.vectorPath[currentWayPoint] - transform.position; // gives us a path to go to. The player to the enemy where the enemy will follow the player.
     dir *= speed * Time.fixedDeltaTime;

     // move the A.I

     rb.AddForce(dir, fmode);

     float dist = Vector3.Distance(transform.position, path.vectorPath[currentWayPoint]); 

     if (dist < NextWayPointDistance)
     {
         currentWayPoint++;
         return; 
     }
 }

}

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 mlnczk · Nov 19, 2018 at 08:30 AM

You need to FindObjectOfType() first. Remember that findobject help you get to the object itself ( if it exists already ) and get components lets you get inside the object's actual components. So you can first in Awake() FindObjectOfType() and then on Start() set his target for example.

Comment
Add comment · Show 2 · 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 MKARSOTNEORS · Nov 20, 2018 at 07:15 AM 0
Share

Could you put this into a coded version if possible? You don't need to do.

avatar image mlnczk MKARSOTNEORS · Nov 20, 2018 at 08:20 AM 0
Share
 //enemyscript that you respawn
 
 private GameObject target;
 
 private void Awake(){
    if (target == null){
    target = GameObject.Find("TargetName"); //if it doesnt work then try
    target = FindObjectOfType<TargetScriptName>();
     }
 }
 //do stuff with your target
 
 
 
avatar image
0

Answer by ecv80 · Nov 19, 2018 at 08:05 AM

 EnemyAI prefabInstanceEnemyAI=prefabInstance.GetComponent<EnemyAI>();
 prefabInstanceEnemyAI.target=targetTransform;
 //or
 prefabInstance.Getcomponent<EnemyAI>().target=targetTransform; //for short

Tell me if I missed the point in order to improve the answer.

P.S.: use code tags! :)

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 MKARSOTNEORS · Nov 20, 2018 at 07:12 AM 0
Share

I tried the code. It wouldn't work for me.

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

101 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

Related Questions

AI Pathfinding BCE0019: 'GetComponent' is not a member of 'Object'. 1 Answer

Plese help me guys! BCE0019: 'GetComponent' is not a member of 'Object'. 1 Answer

Delete a specific instantiated prefab by its vector3 coordinates? 0 Answers

How do I get the Player's Health Script as Component? 1 Answer

How find Object after Instantiate in scene 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