Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity 3D keeps on crashing upon code run.

Discussion in 'Editor & General Support' started by kitoliwa, Feb 7, 2015.

  1. kitoliwa

    kitoliwa

    Joined:
    Feb 7, 2015
    Posts:
    6
    I am new to Unity 3D. I'm currently learning C# from a book. Loads of fun. However, I ran into a problem today.

    (I have looked for the answer to this, but I cant seem to find a clear solution. I am sorry if my question will be noobish in nature and/or if the question is simple, has been answered already.)

    I am experimenting with rotating a 3D cube. I created a 3D cube on the Scene panel, then I created a C# script on the Assets panel and assigned the script to the 3D cube. I am trying to run code on the 3D cube in Unity 3D to make it rotate. If I use the following, it works fine. It rotates once;

    using UnityEngine;
    using System.Collections;

    public class Example : MonoBehaviour {
    // Use this for initialization
    void Start () {
    cubeRotate ();
    }

    // Update is called once per frame
    void Update () {
    }

    void cubeRotate() {
    bool SomeBool = (1 == 1);
    if (SomeBool) {
    transform.Rotate (new Vector3 (45f, 0f, 0f));
    }
    }
    }

    However, after changing the code again in MonoDevelop to rotate the 3D cube some more and then hitting the play button on Unity 3D, the whole program crashes. It won't respond, and I have to use the "Task Manager" to quit. This is the code that crashes Unity 3D;

    using UnityEngine;
    using System.Collections;

    public class Example : MonoBehaviour {
    // Use this for initialization
    void Start () {
    cubeRotate ();
    }

    // Update is called once per frame
    void Update () {

    }
    void cubeRotate() {
    bool SomeBool = (1 == 1);
    float i = 10;
    while (SomeBool) {
    transform.Rotate (new Vector3 (i, 0f, 0f));
    i = i + i;
    }
    }
    }

    I am aware that my code might be wrong. The problem is, why should that crash Unity 3d?
    By the way, clicking on tabs or buttons in Unity 3D is not like in other programs for me, it must be clicked hard, and sometimes many times. I don't know if this may be a clue, just thought I'd mention.

    My pc: Asus intel core i5, nvidia 840m, 4gb ram
    No other programs are running while I'm using Unity 3D.
     
  2. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    So, your cubeRotate() function has a while loop. There is no way for the while loop to terminate, so when you press Play this code runs, and stops anything else from happening. I suspect Unity isn't crashing but locking up. Usually you'd have rotation code inside Update. Update is called every frame, so you could rotate the subs by one degree every frame. You'll probably want to read up on time.deltaTime.
     
  3. kitoliwa

    kitoliwa

    Joined:
    Feb 7, 2015
    Posts:
    6
    I get it now, thanks! :)