GUI Toggle and rotation???

Hello all,

i am a newbie in this Unity awesomeness... i have this simple code to rotate a given game object:

function Update() { transform.Rotate(0, 20 * Time.deltaTime, 0); }

it works quite well, just the way i want it to happen. however i couldnt figure out how to connect GUIToggle button with this code. what i mean is, when i toggle the GUIToggle button on, the cube starts to rotate, when untoggle it stops...

is there ay way to do this with using the Gui Toggle button?

1 Answer

1
using UnityEngine;
using System.Collections;   

public class rotate : MonoBehaviour 
    {
        private bool _rotation;

        void Update()
        {
         if(_rotation)
            transform.Rotate(0, 20 * Time.deltaTime, 0);
        }

        void OnGUI() 
        {
            _rotation = GUI.Toggle(new Rect(10, 10, 100, 30), _rotation, "Rotation");
        }
    }

Yes, this code is C# and you are trying to use it as a JavaScript...

Actually, it's probably because there's no 'using' directive and MonoBehaviour is unqualified.

sry. I didn't see rotate.cs; You need to add using UnityEngine; using System.Collections; on the top of this script, before public class rotate : MonoBehaviour

I mada an edito on my answer, copy this new code and it will work.

Yes...you can do the same thing. Make a new boolean (bool) and place the new if stream inside the OnGUI function.