Gravity invert not working right...

I am using a script to invert the gravity of a single object when I press the “e” key, but if that object happens to be touching another object at the time, the second object also has its gravity inverted, which I don’t want to happen. Here is the script:

var speed = 10;
function Update ()
{
if(Input.GetKeyDown(“e”))

    if(speed > 0)
    {
        Physics.gravity = Vector3(0, speed, 0);
        speed = -10;
        moveAll();
    }
    else
    {   
        Physics.gravity = Vector3(0, speed, 0);
        speed = 10;
        moveAll();
    }
}

function moveAll()
{
var objectsToEffect = GameObject.FindGameObjectsWithTag (“ObjectToEffect”);
for (var object in objectsToEffect) {

}

I am not very good at scripting, so this is all pretty foreign to me. I have put the tag “ObjectToAffect” on just the object I want, but it still affects objects touching it.

Physics is the base class for all physics in your project, meaning you’re not addressing any specific rigidbodies. For the objects you want to affect by inverted gravity, use:

rigidbody.AddForce(-Physics.gravity, ForceMode.Acceleration);

Remember to turn off gravity:

rigidbody.useGravity = false;

What you do is reversing the force setup in Physics.gravity (Edit > Project Settings > Physics). Using ForceMode.Acceleration means that you ignore the mass to the continuous applied force.

In your case it would be great to send a message to all these objects that should be affected by inverse gravity,

//Script #1 on each rigidbody with inverse gravity ability
var invertGravity : boolean = false;
function FixedUpdate () {
	if (invertGravity) rigidbody.AddForce(-Physics.gravity, ForceMode.Acceleration);
}
function InvertGravity () {
	invertGravity=!invertGravity;
	if (invertGravity) rigidbody.useGravity=false; else rigidbody.useGravity=true;
}

//Script #2 sending to all rigidbodies with specific tag
private var invertGravityObjects : GameObject[];
function Start () {
	invertGravityObjects = GameObject.FindGameObjectsWithTag("ObjectToAffect");
}

function Update () {
    if (Input.GetKeyDown(KeyCode.E)) SendToAllInversePhysObjects();
}

function SendToAllInversePhysObjects () {
	for (go in invertGravityObjects)
		go.SendMessage("InvertGravity", SendMessageOptions.DontRequireReceiver);
}

This is working perfectly! Thank you so much for your help!