getting rigidbody of the objects inside the zone

Hello,

Im trying to make a force field that pushes objects inside, i use collider enter than try to get their rigidbodies and apply force to it. Im having trouble with connecting the force to the rigidbodies of objects.

here is my code, any help is appreciated.

using UnityEngine;
using System.Collections;

public class gravityfmk : MonoBehaviour
{

	public float gforce= 5;
	public Vector3 z3 = new Vector3( 0, 0, 1);

	void OnCollisionEnter (Collision gravity)
	{
		if(gravity.gameObject.name == "Asteroid-001-LowPoly" || gravity.gameObject.name == "Asteroid-002-LowPoly")
		{
			GetComponent<Rigidbody>().AddForce (z3 * gforce);
		}
	}

}

Oh, it’s you again.

I’m guessing this is what you’re looking for?

gravity.gameObject.GetComponent<Rigidbody>().AddForce (z3 * gforce);

You can’t just randomly do GetComponent<>() without a reference to the game object you getting the component from.