I'm making a game in Unity where 'enemies' spawn in (controlled from a script on a player), I'm using prefabs of the enemies, I have got the enemies to spawn in but the pathfinding isn't working. I know why - it's that when you reference the player transform while the enemy is in the scene there's a link but once the enemy is a prefab it can't access the player's components as it's not in the scene. I did a bit of researching and found the answer - I needed to instantiate the enemies like normal, but set this to a variable, enemyGO
for example, and then access the enemy's AIDestinationSetter
script through enemyGO.GetComponent<AIDestinationSetter>().target = gameObject
- this would assign the target
value of the pathfinding target to the player (gameObject
). This would all work well, but it kept saying that it could no find the AIDestinationSetter component. This, after a while of tinkering, proved to be (I think) because in the script, the AIDestinationSetter
is within a namespace.
Here's the AIDestinationSetter
code:
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; // THIS IS THE VARIABLE I'M TRYING TO ACCESS
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;
}
}
}
and here's where I'm attempting to access the script from:
public GameObject enemyGO; // creating the empty variable
// and then further on in the script:
Vector2 spawnPos = GameObject.Find("Player").transform.position;
spawnPos += Random.insideUnitCircle.normalized * spawnRadius;
enemyGO = Instantiate(gameObject, spawnPos, Quaternion.identity);
enemyGO.GetComponent<Pathfinding>().target = spawnPos;
Please help if possible, I've been trying to solve this for hours! Thanks
You probably rather ment
GetComponent<Pathfinding.AIDestinationSetter>
Or have
using Pathfinding;
on top of your script and simply use
GetComponent<AIDestinationSetter>
you are currently trying to give a namespace to GetComponent
which obviously won't work