1

I am a beginner to Unity Development and came through a task of rotating cube Randomly. I saw many sources but was getting confused about connecting the Cube to the script on Button button click.

Currently, I am doing this:

void Update()
{
    transform.rotation = Random.rotation;
}

on the script and adding it to the cube but doing it on button click sounds tricky. Suggestions are appreciated.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Nikul Vyas
  • 363
  • 3
  • 7
  • 30

1 Answers1

1

If you put a collider on the cube, you can add to the cube's script

void OnMouseDown() 
{
    transform.rotation = Random.rotation();
}

which will be run whenever the collider detects a click.

Daxtron2
  • 1,239
  • 1
  • 11
  • 19
  • Works perfectly fine thank you. – Nikul Vyas Mar 14 '18 at 17:20
  • Hey, no problem at all. We all gotta start out somewhere. If you don't already know and use it, check out [the unity documentation](https://docs.unity3d.com/Manual/index.html) for lots of information. – Daxtron2 Mar 14 '18 at 17:23
  • Actually many of the docs have been rolled back too because of Unity 5's new upgrades like now mesh filter & rigid body cannot be used on the same gameobject. – Nikul Vyas Mar 14 '18 at 18:24