Collider not letting me AddForce...

Hi Guys,

I’m using a Physics.OverlapSphere… It’s working okay in itself. I’ve Debug.Logged it and it’s recording a long array of collected objects… The issue is when I try to add force to the collected objects the while loop breaks and it doesn’t add any force. I think that means the game doesn’t recognising me adding force… Does anyone know what’s happening?

	IEnumerator Shoot()
	{
		Debug.Log("STARTED");

		while( timeElapsed < shotsToFire )
		{
			Debug.Log("CONTINUED");

			Instantiate(pushPfx, transform.position, Quaternion.identity );

			Collider[] hitColliders = Physics.OverlapSphere(transform.position, 100000);

			int i = 0;

			while (i < hitColliders.Length)
			{
				Debug.Log("HIT: " + i);
				if( hitColliders[i] != this.gameObject);
				{
					Debug.Log("Meets Criteria");
					hitColliders[i].gameObject.transform.rigidbody.AddForce( (transform.position - hitColliders[i].transform.position) * 10000, ForceMode.Impulse);//AddExplosionForce(pushForce, transform.position, 2000);
				}

				i++;
			}

			timeElapsed++;

			yield return new WaitForSeconds(fireInterval);
		}

		Debug.Log("BLOODY ENDED");
	}

Can someone help?

maybe obvious but… did you check if all the objects in hitColliders actually have a rigidbody?
also don’t have to go through gameObject.transform you can just go from collider to rigidbody like hitColliders*.rigidbody…*

Okay I just tried that… Same issue. Here:

	IEnumerator Shoot()
	{
		Debug.Log("STARTED");

		while( timeElapsed < shotsToFire )
		{
			Debug.Log("CONTINUED");

			Instantiate(pushPfx, transform.position, Quaternion.identity );

			Collider[] hitColliders = Physics.OverlapSphere(transform.position, 100000);

			int i = 0;

			while (i < hitColliders.Length)
			{
				Debug.Log("HIT: " + i);
				if( hitColliders[i] != this.gameObject  hitColliders[i].GetComponent<Rigidbody>() != null);
				{
					Debug.Log("Meets Criteria");
					hitColliders[i].gameObject.transform.rigidbody.AddForce( (transform.position - hitColliders[i].transform.position) * 10000, ForceMode.Impulse);//AddExplosionForce(pushForce, transform.position, 2000);
				}

				i++;
			}

			timeElapsed++;

			yield return new WaitForSeconds(fireInterval);
		}

did you try a for loop instead of the while?

If the force code is being run, I expect the problem is elsewhere.

Two obvious reasons come to mind:

  1. You have something setting the velocity elsewhere
  2. The rigidbody is kinematic