Enable rigidbody gravity,Enable Rigid body gravity

for my school assignment I want objects with a certain tag to have rigidbody gravity enabled when a raw image with button component added to it is clicked but not before hand. I know how to make a button change scene but I cant find anything about this. Help Please.,I want to make blocks activate rigidbody gravity when a rawimage with button component is clicked. but not before hand.

Heyo,

You can do something like this:

private Rigidbody rb;

private void Start() {
    rb.useGravity = false;
    rb = GetComponent<Rigidbody>();
}

public void EnableGravity() {
    rb.useGravity = true;
}

The main bit will be with the EnableGravity function. If you have created one of Unity’s UI buttons in your scene and you click on it, you should be able to find its Button component (in the inspector window). Inside of that component there is an OnClick event. If you click on that + button and fill in the information, you should be able to add the EnableGravity function to this OnClick event.


This was assuming that the button on each rigidbody though. If you are wanting a single button to enable gravity on a number of rigidbodies, there are a couple ways of doing it. You’ll want to do something very similar to what I described above, but we will want to put something different inside of the buttons OnClick event. What you could do is [find all gameobjects that have the tag you are looking for][1], grab their rigidbody components, and enable gravity, or you could make a list and populate it with objects yourself. It would then use this list of objects and enable gravity on those. Here is a quick example of each:


Using an array of predefined rigidbodies

// In Unity's inspector window, populate with scene objects that you want to have gravity enabled on
public Rigidbody[] objects;


// Go through each rigidbody in the array and enable gravity
public void EnableGravity() {

    for (int i = 0; i < objects.Length; i++) {
        objects*.useGravity = true;*

}

}
Using GameObject.FindGameObjectsWithTag
// Go through each rigidbody in the array and enable gravity
public void EnableGravity() {

// Find all objects with a specific tag
GameObject[] targets = GameObject.FindGameObjectsWithTag(“myTag”);

for (int i = 0; i < targets.Length; i++) {
Rigidbody rbComp = targets*.GetComponent();*
rbComp.useGravity = true;
}

}
----------
Just a reminder that if you are using Unity’s UI buttons, you will need to make sure you assign this function to the button’s OnClick event. Otherwise it’ll never happen. Let me know if you have further questions!
_*[1]: https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html*_

Disable the gravity for rigid bodies.
123369-disablerigid.png


Then add a ‘Constant Force’ component to your game objects you wish to have gravity.
123370-cforce.png


When you click the button, access the Constant Force component’s force parameter and give it the gravity you desire. Most likely something like this:

ConstantForce cf;
cf = gameObject.GetComponent<ConstantForce>();

if (click)
{
     cf.force = new Vector3(0, -9.8f, 0);
}

Hi @coltanjamesshaw02

Well if this is a school assignment, you should be learning and doing research (= googling and making notes) - right? I can help a bit though:

For finding objects google for: “unity find objects with tag”

For creating UI elements from code:

https://docs.unity3d.com/Manual/HOWTO-UICreateFromScripting.html

For adding click listeners to buttons google for: “unity add button listener from code”

To get components: