- Home /
Why isn't a Ai Waypoint Spawning???
I am making a Ai waypoint system. But a waypoint is not spawning in the scene, I don't know what i'm doing wrong. Please Help
using UnityEngine;
using System.Collections;
public class AiWayPointManager : MonoBehaviour {
float WayPointInstantiateTimer = 5;
GameObject AiWayPoint;
GameObject Ai;
GameObject LastWaypoint;
// Use this for initialization
void Start () {
AiWayPoint = Resources.Load("Waypoint") as GameObject;
Ai = GameObject.Find("AI");
}
// Update is called once per frame
void Update()
{
LastWaypoint = GameObject.FindWithTag("Waypoint");
var LastWaypointPos = LastWaypoint.transform.position;
var AiPos = Ai.transform.position;
if (GameObject.FindWithTag("Waypoint") == null)
{
var SpawnWayPointAtStartOfLevel = Instantiate(AiWayPoint, AiPos + transform.forward * 2, transform.rotation) as GameObject;
}
WayPointInstantiateTimer -= Time.deltaTime;
if (WayPointInstantiateTimer <= 2)
{
GameObject SpawnWayPoint = Instantiate(AiWayPoint, LastWaypointPos + transform.forward * 2, transform.rotation) as GameObject;
WayPointInstantiateTimer = 5;
Debug.Log(WayPointInstantiateTimer);
}
if (Vector3.Distance(transform.position, LastWaypointPos) <= 0)
{
// AiOnWayPointPos();
}
}
/*void AiOnWayPointPos()
{
gameObject.active = false;
}*/
}
Does the Console show anything?
If yes then you should look at the Hierarchy to see if there is new object spawned.
If there is a new object spawned then the problem is at "LastWaypointPos + transform.forward * 2" (spawned too far/too high/underground ...)
@GarretLawrence Thx for the comment. But no errors and in the hierarchy, the first waypoint is never spawning(SpawnWayPointAtStartOfLevel)
Well... actually if this line
"LastWaypoint = GameObject.FindWithTag("Waypoint")"
return an object then no way
"GameObject.FindWithTag("Waypoint") == null"
@GarretLawrence can you elaborate on that last comment please?
I meant if "GameObject.FindWithTag("Waypoint")" is null then system will throw null reference exception.
Because if it is null then LastWayPoint is also null, and when you try to access transform.position from a null GameObject (LastWayPoint) your code will break.
As you said, there is no error at all, so there must be a gameobject with "Waypoint" tag always exist from the beginning.
So there is no way for this line of code "var SpawnWayPointAtStartOfLevel = Instantiate(AiWayPoint, AiPos + transform.forward * 2, transform.rotation) as GameObject;" be executed.
Your answer
Follow this Question
Related Questions
Sidescroller AI/Pathfinding/Flags/Waypoints 0 Answers
How do you move a character to another waypoint after landing on a specific waypoint 1 Answer
Make player follow a waypoint path but still be able to control player movement 1 Answer
Easy WayPoint System - Character climbing at waypoint 0 Answers
waypoint system help 1 Answer