Enemy gameobject disappears on transform.position

SOLVED: So the enemy was just inside of the cubes, because of the waypoint origin being the center of the cube instead!

For some reason, when using (I ended up copying line-for-line just to be sure), the specific line:

transform.position = waypoint.transform.position;

makes the enemy game object disppear

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyMovement : MonoBehaviour
{
[SerializeField] float dwellTime = 1f;

// list are used if the collection size varies, instead of an array
[SerializeField] List<Waypoint> path;

// Start is called before the first frame update
void Start()
{
    StartCoroutine(FollowPath());
    print("Hey I'm back at Start");
}

IEnumerator FollowPath()
{
    print("Starting patrol...");
    foreach (Waypoint waypoint in path)
    {
        transform.position = waypoint.transform.position;
        print("Visiting: " + waypoint);
        // todo: wait a second!
        yield return new WaitForSeconds(dwellTime);
    }
    print("Ending patrol");
}

}

It is already iterating the rest of the foreach loop correctly and printing out the positions in the console. But the enemy object itself disappears. I’ve checked that the Enemy and Body both had their transformations reset prior to childing the body to the enemy and to attach the script to the enemy (parent) object.

As long as I comment it out, the enemy object will show up in the play window, but not move (since the transform portion of the loop was commented out.

The Waypoint script is correctly being associated with the blocks as well

I’m glad you solved the problem. Well done! :slight_smile:


See also:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms