- Home /
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;
}
}
}
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.
Could you put this into a coded version if possible? You don't need to do.
//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
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! :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612182350im_/https://answers.unity.com/themes/thub/images/avi.jpg)