Spawn with incorrect direction. some of the time

so, unknown complexity, i think performance, and target is kind of a bs subject for this, as i don’t think it technically matters. I have a distinct issue, and i’m going to try to give all the relevant info for it, so here it goes.

quick description: when I begin play, i have a number of enemies that are to spawn after a certain amount of time, which works fine. I had this problem before, and changing my update of time.deltatime into fixed update solved it, but i’m not using that for this one. when they spawn, they are instantiated to spawn in a specific direction, specifically spawner point 1’s direction. some of them in the order start not doing that.

Edit: Maybe the question was too implied, so here it is: why is it doing this, and/or does anyone know how to solve it?

whytheaf

that shows what i mean, sometimes it’s one, sometimes every other one.

all relevant code (that i’m aware of):

spawners at the bottom

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class NWaveWaypoints : MonoBehaviour
{
    public GameObject[] ships;
    public Transform[] spawnerPoints;
    public int counter;
    float spawnStart;
    float spawnTimer = 0.5f;
    // Start is called before the first frame update
    void Start()
    {

    }

    private void OnEnable()
    {
        counter = 0;
        spawnStart = Time.time;

    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (Time.time > spawnStart)
        {
            if (counter < ships.Length)
            {
                GameObject clone = Instantiate(ships[counter], spawnerPoints[0].transform.position, spawnerPoints[0].transform.rotation);//, spawnerPoints[0].transform.position, Quaternion.Euler(0, 0, 0));
                //clone.transform.position = spawnerPoints[0].transform.position;
                //clone.transform.rotation = spawnerPoints[0].transform.rotation;
                clone.GetComponent<Path1>().waypoints = spawnerPoints;
                clone.GetComponent<Path1>().rotspeed = 3f;
                clone.GetComponent<Path1>().speed = 1.13f;
                clone.GetComponent<Path1>().AssignWayPoints();
                spawnStart = Time.time + spawnTimer;
                counter++;
            }
        }
        if (counter >= ships.Length)
        {
            gameObject.SetActive(false);
        }
    }
}

the spawners on top

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class WaypointSpawns : MonoBehaviour
{
    public GameObject[] ships;
    public Transform[] spawnerPoints;
    public int counter;
    float spawnStart;
    float spawnTimer = 0.5f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    private void OnEnable()
    {
        counter = 0;
        spawnStart = Time.time;

    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (Time.time > spawnStart) {
            if (counter < ships.Length)
            {
                GameObject clone = Instantiate(ships[counter], spawnerPoints[0].transform.position, spawnerPoints[0].transform.rotation);
                //clone.transform.position = spawnerPoints[0].transform.position;
                //clone.transform.rotation = spawnerPoints[0].transform.rotation;
                clone.GetComponent<Path1>().rotspeed = 1.75f;
                clone.GetComponent<Path1>().waypoints = spawnerPoints;
                clone.GetComponent<Path1>().AssignWayPoints();
                spawnStart = Time.time + spawnTimer;
                counter++;
            }
        }
        if (counter >= ships.Length) {
            gameObject.SetActive(false);
        }
    }
}

enemy ship

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

public class Path1 : MonoBehaviour
{
    public Transform[] waypoints;
    [SerializeField] Vector3[] points;
    public int currentWP = 0;
    public Vector3 wpTarget;
    public Rigidbody rb;
    public float speed;
    public float rotspeed = 1f;
    float waypointDistance = 0.5f;
    // Start is called before the first frame update
    void Start()
    {
        
    }
    public void AssignWayPoints()
    {
        points = new Vector3[waypoints.Length];
        for (int i = 0; i < waypoints.Length; i++)
        {
            points[i] = waypoints[i].transform.position;
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (points.Length > 0)
        {
            // i need current wp to be a number form 0 to length -1
            wpTarget = points[currentWP];
            Debug.Log(currentWP);
            if (Vector3.Distance(transform.position, wpTarget) < waypointDistance)
            {
                if (currentWP == waypoints.Length - 1)
                {
                }
                else
                {
                    currentWP++;
                }
            }
            TurnTowardWayPoint();
        }
    }
    void FixedUpdate()
    {
        rb.velocity = transform.forward * speed;
    }
    void TurnTowardWayPoint()
    {
        if (wpTarget - transform.position != Vector3.zero)
        {
            Quaternion lookatWP = Quaternion.LookRotation(wpTarget - transform.position);
            transform.rotation = Quaternion.Slerp(transform.rotation, lookatWP, rotspeed * Time.deltaTime);  
        }
    }
}

now, i say this is all the relevant code, because i’m reasonably sure after checking multiple times that these are the only actual scripts running in the scene.

i can see no reason that any of the ships should spawn in any direction except what the first spawn points direction is, and for reference to that…

small edit: there appears to be some small lag spike when one spawns off angle, but only in editor, no spike in a build.

So, should i assume from no responses that im correct and this shouldnt be happening? Or that its a feature, and unity does this on its own?

I strangely found a solution, and it happened to be “close unity and open it again” but that seems like its leaving an underlying question.

No. If anyone thought that, then they would have just said so. No responses usually means that anyone who read your question got bored and gave-up before coming to any conclusion. There comes a point when your code base gets big enough that it becomes difficult to follow just by reading through.

At this point I can only suggest debugging and isolating the code where you think the problem might be. For example, can you be certain that objects spawn in the incorrect direction, or could they possibly be set to the wrong direction by your Path1 script? or even something else? Are you sure that the rotation that up pass in to Instantiate is correct to begin with? If you set the rotation on a separate line of code (like you have commented-out) does that fix the problem?

Another thing I suggest is that it looks like you have two scripts that are almost identical. I’m certain you could combine these in to one. Fewer scripts means there fewer places to troubleshoot if something goes wrong.

Edit: One more thing- why are you using Fixed Update to spawn these whatever-they-are? Have you tried Update?

Update made them spawn erratically, so i moved to the smoother fixed update. Again, there was no error in the code that i could see, considering closing and opening unity seemed to have solved it, im just curious as to why that should make a diffence now, i guess. Kinda like another post i made elsewhere amout rmb rotating and letting go anywhere but the scene breaks my ability to manipulate the scene with the mouse button. Restarting unity is obnoxious to be something to debug with.

If Restarting the Unity editor magically solves the problem, one possible explanation is-

Unity tries to save you time by not recompiling every single script every time you enter play mode. Instead it recompiles only scripts that have been updated. Occasionally, something gets out-of-sync and a script can get updated without the editor registering that it’s been updated. If you happen to know which script it is you can also right-click on the script file and select “reimport”. That usually fixes the problem.