Explosions Problem...

this seems like a simple question, but i cannot get this to work:

var explosionPower = 10.0;
var explosionRadius = 10.0;

function explosionForce(explosionPos)
{
	var colliders : Collider[] = Physics.OverlapSphere(explosionPos, explosionRadius);
	
	for (var hit in colliders)
	{
		if (!hit)
		continue;

		if (hit.rigidbody)
		{
hit.rigidbody.AddExplosionForce(explosionPower, explosionPos, explosionRadius, 3.0);
		}
	}
}

yes, the sourrounding objects do have rigidbodys, and OverlapSphere is finding a load of objects, and then apparently the explosion force is being added to each one… but i am seing no effect of this when this is called.

there must be something simple i have overlooked?

Thanks.
Cav.

Have you tried making explosionPower like, 1000000?

-Jon

thanks! it works now…i had already put that number right up, but it seemes that when im changing the var numbers in my code when the scene is playing the same number in the inspector is back to the original number (10), so had changed itself back, strange…has anyone else had that problem?

When you expose a variable to the inspector, then the value of the inspector overrules the variable in the script. (Otherwise you couldnt tweak the value in the inspector).

When you expose a variable to the inspector it basically acts like a default value, if you dont want that behaviour, just make the variable private, then the inspector has no control over it.

ah, my mistake, i was under the impression that the exposed variable could be changed both ways…

thanks again!

… argh, this has been driving me nutz for weeks now. I keep on changing the value of a variable in the script, run the app and don’t see any change, so I change the variable in the script again. Then after banging my head against the wall because I can’t figure out what’s wrong with my code, I then remember that I have to change the value of the variable in the -editor-… jeesh… I’m sure Otee’s approach is better, but years of doing things differently is a hard habit to break!

If you don’t want to change the variable in the inspector just make the variable private.

private var p = 10.0;

Modifyable values in the inspector are useful in a lot of cases but of course not for everything. So use it accordingly. A good rule for when a value should be in the inspector is:

  1. You attach the script to more than one game object and the value should be different in each of them.

  2. You need to quickly tune the value and you don’t want to go to your script and edit the value until you find the right one.

  3. You want to make your script reusable and you can foresee that the value might need to be changed when you use it in another project / scene.

  4. You want the script to be used by other people (maybe even people who can’t script). And they need to adapt tweak the values to make it fit in their project.

Thanks Nicholas, understood, I’ve just got some old habits from years of doing things “the old way”. :smile:

Yeah, I’ve done the “banging head against wall” thing a few times because of that too. :wink: Obviously it’s a great feature, but those old habits are annoying, aren’t they? Fortunately I seem to have ditched that habit by now.

–Eric

What I usually do is to make pretty much everything public, as that is much easier for tweaking.

When I’m done, I copy in the values I ended at back to the script, and make some of them private.

Yup, that’s exactly what I thought I’d do in the future. Thanks!