Rotate cube on button click in Unity3D

3.1k Views Asked by At

I have created one cube. I want to rotate that cube on button click. I created script for rotating cube and another script for button click. Now how can I make that cube rotate on this button click.

Code for Button:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ButtonInput1 : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame


    Button button=null;
    void Rotate()
    {
        a++;
    button.onClick.AddListener (delegate() {    PlayerController.CubeRotate ();});

    }
}

Code for RotateCube:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

    // Use this for initialization
    // Update is called once per frame
    int a=1;
    public  void CubeRotate () {
    if (Input.GetButton("Button1")) {
        a++;
            //transform.Rotate ( new Vector3(15,30,45) * Time.deltaTime);
    }
        if (a % 2 == 0) {
            transform.Rotate ( new Vector3(15,30,45) * Time.deltaTime);

        }
    }

}

I just need to know, how to call cube's script on this button click. Is there any other way to achieve this?

1

There are 1 best solutions below

0
On

Thats funny, i actually just answered this question worded exactly like yours asked by another person. Rotate object continuously on button click Unity3D