Setting gravityScale through script issues

Hey there,

I’m trying to set an object to change it’s gravityScale by clicking a button, but it doesn’t seem to respond and I’m not sure why.

using UnityEngine;
using System;
using System.Collections;

public class Oxygen_Script : MonoBehaviour {


	public GameObject[] goList;
	public string atmosphere;

	void Start () {

		goList = (GameObject[])FindObjectsOfType (typeof(GameObject));
	
	}
	
	// Update is called once per frame
	void Update () {


	
	}

	void OnMouseDown()
	{

		int max = goList.Length;
		for (int i = 0; i < max; i++) 
		{
			Debug.Log(goList[i].gameObject.name);
			if (goList[i].gameObject.name == "Bubble") 
			{
				//Rigidbody2D rb = goList[i].gameObject.GetComponent<Rigidbody2D> ();
				//rb.gravityScale = 0;
				goList[i].rigidbody2D.gravityScale = 0;
			}
		}
	}
}

I’ve also tried setting the gravityScale to 0.0f it doesn’t work either. the current setting for the bubble object is -0.25

Any feedback would be greatly appreciated

Are you actually looking at the public gravity scale for the object? Im currently struggling with making an object hover. It works to an extent. I can see the gravity scale set to zero, but there is still some sort of downward (maybe in your case upward since your gravity is negative to begin with) force im fighting against. Its causing rapid downward then upward movement thrashing.

public class PlayerBehavior : MonoBehaviour {
Vector3 jumping;
public int jumpingPower;
float currentY;

// Use this for initialization
void Start () {

}

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

}

void ControllerInput()
{
Vector3 move = Vector3.zero;
if (Input.GetKey(KeyCode.D))
move += Vector3.right3;
if (Input.GetKey(KeyCode.A))
move += Vector3.left
3;
if (Input.GetKeyDown (KeyCode.Space) jumping.y == 0) {
jumping = (Vector3.up*jumpingPower);
}
if (Input.GetKeyDown (KeyCode.LeftShift)) {
jumping.y = 0;
rigidbody2D.velocity.Set(0, 0);
rigidbody2D.gravityScale = 0;
}
if (Input.GetKey (KeyCode.LeftShift)) {
move.y = 0;
rigidbody2D.velocity.Set(0, 0);
move.y += rigidbody2D.velocity.y * -1.0f;
}
if (Input.GetKeyUp (KeyCode.LeftShift))
rigidbody2D.gravityScale = 1.98f;

transform.position += (move + jumping) * Time.deltaTime;
//transform.position += Vector3.down* 9.8f * Time.deltaTime;
jumping.y *= .8f;
if (jumping.y < .2f)
jumping.y = 0;
}
}

OP: are you sure your script is finding your “Bubbles” game object? Put a Debug.Log (or something) in that section to confirm it’s ever called in the first place.

Other commenter: try not to hijack threads with your own unrelated issues. To solve your problem, know that all physics forces should only be applied in FixedUpdate(), not Update(). Also, it looks like you are attempting to do several competing things (set a velocity directly, then set a transform position directly). These will absolutely interfere with each other. Also keep in mind gravity isn’t a translation of 9.8m per second but an acceleration by that amount.

I gave the most specific answer I could, given my experience.

Ok so I’ve decided to rework my script to see if I can get gravityScale to set at all, what I have right now is a script directly applied to the bubble object that in theory should change the gravityScale, but it doesn’t seem to want to change anything.

using UnityEngine;
using System.Collections;

public class Bubble : MonoBehaviour {


	public string atmosphere;
	public float oxygenVal = 0.0f;
	public float heliumVal = 0.25f;
	public float hexaneVal = -0.25f;

	// Use this for initialization
	void Start () {

		this.rigidbody2D.gravityScale = oxygenVal;
		atmosphere = "Helium";
	
	}
	
	// Update is called once per frame
	void Update () {

		if (atmosphere == "Helium") 
		{
			this.rigidbody2D.gravityScale = heliumVal;	
		}
		if (atmosphere == "Hexane") 
		{
			this.rigidbody2D.gravityScale = hexaneVal;
				
		} 
		else 
		{
			this.rigidbody2D.gravityScale = oxygenVal;
				
		}
	
	}
}

You just have a simple logic error in your code.

You’ve said

  1. If Helium then…

  2. If Hexane then…

  3. Else …

The compiler will read 3) only as it relates to 2). So your Else statement will always execute unless Hexane is true (even if 1 executes).

Else statements only consider the If statement directly before them.

You just need to modify the Hexane check to “else if”.

To hover something I would simply to something like turning off gravity and/or freezing Y movement, either of which can be done with scripting as we know. Otherwise something has to keep tweaking the upward ConstantForce until it can be determined that Y movement is minimized, if you really desire the physics being true to reality.

OR no ELSE statement, just start out saying it’s Oxygen, only change if it’s one of the other two.

Just to note, that post you’re replying to is over 5 years old. :slight_smile:

1 Like