Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug Unity crashes when play

Discussion in 'Editor & General Support' started by SpyderManToo, Feb 5, 2021.

  1. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    Im making a 3d aim trainer and I recently added a moving platform.
    heres the code
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class TargetRotateX : MonoBehaviour
    {
    public GameObject target;
    public GameObject rotatePoint;

    public float rotateSpeed;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
    while(true)
    {
    StartCoroutine("platformRotate");
    }

    }

    IEnumerator platformRotate()
    {
    transform.RotateAround(rotatePoint.transform.position, target.transform.up, rotateSpeed * Time.deltaTime);
    yield return new WaitForSeconds(Random.Range(3, 10));
    transform.RotateAround(rotatePoint.transform.position, target.transform.up, -rotateSpeed * Time.deltaTime);
    }
    }

    it should change direction every so often but whenever I try to run the project, it freezes and i have to quit the application. help
     
  2. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    help me pls
     
  3. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Use CODE tags when posting to the forum. If you make your code difficult to read, few people will bother. But your problem is here:
    Code (csharp):
    1. void Update()
    2. {
    3.     while(true)
    4.     {
    5.         StartCoroutine("platformRotate");
    6.     }
    7.  
    8. }
    You've created what is called an "infinite loop". Don't do that. As written, once you first enter the while loop, it will never exit. So the frame cannot complete, cannot move onto the next frame, and the game will appear to freeze.