stop rotating object with a mouse click

how to stop a rotating object in this coding (cs) below with an mouse click ?

void Update () {
transform.Rotate (new Vector 3(Time.deltaTime*30,0,0));
}

Thank You

here is a simple way to control the rotation

using UnityEngine;
using System.Collections;

public class yourClassName : MonoBehaviour
{
    public bool rotateObject = true;
   
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if(rotateObject == true)
            {
                rotateObject = false;
            }
            else
            {
                rotateObject = true;
            }
        }
   
        if(rotateObject == true)
        {
            transform.Rotate (new Vector 3(Time.deltaTime*30,0,0));
        }
       

    }
}

Hai, really thanks for the help…i will try it later by today & let it know. Thanks again…

did the code work for you?

Hai, sorry i just to check it due to internet problem 7 its work but can i know how if i have many objects & click on it to stop each of it individually? & can i stop it once clicked forever, means if click again it remain stopped?
Really thank you for the help provided…really appreciate it since i really wanted to learn…thank you again…

Ok sorry, i get it to stop it once clicked forever stopped…so just wanted to know who to make each object stop once clicked individually on it? Really thank you for the help provided…really appreciate it since i really wanted to learn…thank you again…

There are a few ways to detect clicks on individual objects. The easiest way is to use the built-in Monobehaviour callback OnMouseDown. This behaves similarly to Start and Update in that Unity will automagically call it at a certain time.

That time being when a GameObject with a collider is clicked, or a GUIElement is clicked.

So give your clickable objects a collider shape, and try using OnMouseDown.

You can reduce the previous code example a bit:

using UnityEngine;
using System.Collections;

public class yourClassName : MonoBehaviour {
    public float rotationSpeed = 30f;

    private bool shouldRotate = true;

    // called once per frame
    private void Update() {
        if(shouldRotate) {
            // rotate x axis by rotationSpeed degrees per second
            transform.Rotate(Vector3.right, rotationSpeed * Time.deltaTime);
        }
    }
   
    // called when this object's collider is clicked
    private void OnMouseDown() {
        // invert shouldRotate
        shouldRotate = !shouldRotate;
    }
}

Hai, thanks for the reply…u mean i just take this coding only? I tried but its rotating but can’t stop if clicked…Thank you

Does the object have a collider?

do you wanted to click on each object to stop one object at a time? or do you want a single click that stops all objects at the same time.

Jeffreyschoch’s script is much easier then mine was. So I modified his script to not allow the gameobjects to rotate after being stopped. His script is setup to stop one object at a time from rotating. Just put this script on each object that you want to stop the rotating.

using UnityEngine;
using System.Collections;

public class yourClassName : MonoBehaviour {
    public float rotationSpeed = 30f;

    private bool shouldRotate = true;

    // called once per frame
    private void Update()
    {
        if(shouldRotate) {
            // rotate x axis by rotationSpeed degrees per second
            transform.Rotate(Vector3.right, rotationSpeed * Time.deltaTime);
        }
    }

    // called when this object's collider is clicked
    private void OnMouseDown()
    {
        // invert shouldRotate
        if(shouldRotate)
        {
        shouldRotate = !shouldRotate;
        }
    }
}

OK, sorry for late reply…my internet having problem to contact… i will try it again by today…really thank you for the help, appreciate your time for learner like me…thank you, will update by today…thanks

ok, no problem

Hai, i tried it but still it can’t be stopped if clicked…its just keep on rotating…i attached the box collider too…is there i need add the"tag" for the object in the inspector setting? Thank you

OK, do this for me. Create a 3d cube. it will come with a collider and everything. Then you need to add the this script to the 3d cube. press play and it should work. If it doesn’t, you’ll need to give us more info about your scene. Something maybe overriding something.
Changing the tag is not needed.

OK, thank you for ur guide…i do it by now & update it by now…thanks

Great!!! Its worked on 3D Cube…thanks…but, is the script won’t work for UI Image (Gameobject)?

I did a drag & drop UI Image in a placeholder…is that script won’t work for 3D object? Here is the script for drag & drop for UI…will it can be used for UI Image? Thank you again for your guide…really wanted to learn…thanks
Here the script of my UI image for drag&drop:

using UnityEngine;
using System.Collections;

public class god : MonoBehaviour {

// Use this for initialization
void Start () {

}

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

}

public void Drag ()
{
GameObject.Find (“Image”).transform.position = Input.mousePosition;
print (“we”);
}

public void Drop()
{
GameObject ph1 = GameObject.Find (“placeHolder1”);
float distance = Vector3.Distance (GameObject.Find (“Cube”).transform.position, ph1.transform.position);
if (distance < 80) {
GameObject.Find (“Cube”).transform.position = ph1.transform.position;
}

}

}

So we have changed from a rotating script to a drag and drop script.
UI gameObjects have Rect Transform. that’s why the script does not work on UI gameObjects.

In the god script, can you tell me how the Drag() and Drop() is getting ran? The god script is nor running them. An outside element could be. so I want to know how you are access the two functions.

Hai, thanks for the reply, i wanted to make a puzzle game, so i get coding from 1 tutorial make it drag & drop…so i just want to enhance it with add a rotation script & stop it when click it to move the object…here is the tutorial i referred…again really thank for the help & time for help to learn something new…thanks
tutorial link :