- Home /
why is part of my script getting called as if it was in the Start function?
so this is the script, it's the basic explosion script from the Unity scripting reference. the problem is that as soon as the game starts, blocks go flying; it completely ignores whether I've pressed the button down or not.
using UnityEngine;
using System.Collections;
public class Explode : MonoBehaviour
{
public float radius = 6.0F;
public float power = 10.0F;
void Update()
{
if(Input.GetKeyDown (KeyCode.E))
{
Debug.Log ("called");
Vector3 explosionPos = transform.position;
Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
foreach (Collider hit in colliders) {
if (!hit)
if (hit.rigidbody)
hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0F);
}
}
}
}
sorry, I meant flying as in propelled by an explosion. I doubt it, nothing happens if the explosion script isn't in place. strangely, the explosion also does not work if IsTrigger is enabled on the collider
and no, the debug log does not output
oh ha, that would explain a few things. I just realized the collider was what was making the rigidbodies move. Now it's just an issue of the explosion script not working... well, thanks
try commenting out just the line that causes the actual explosion, and see if anything still happens?
deactivating the script itself still causes it. it's definitely a collider problem.
Answer by gardian06 · Sep 28, 2013 at 08:02 PM
flying how?
is it possible that you somehow put rigidbodies inside of one another, and they are simply responding to the collision?
does your debug log get outputted?
Answer by Entairex1 · Sep 28, 2013 at 09:58 PM
Hmm you have some strange spacing in your code
using UnityEngine;
using System.Collections;
public class Explode : MonoBehaviour
{
public float radius = 6.0f;
public float power = 10.0f;
void Update()
{
if(Input.GetKeyDown(KeyCode.E))
{
Debug.Log("called");
Vector3 explosionPos = transform.position;
Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
foreach(Collider hit in colliders){
if (!hit)
if(hit.rigidbody)
hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0F);
}
}
}
}
This should be working, if it's not, then it's probably because you have another script causing this problem.
yeah the force just isn't applying for some reason. I guess I'll just have to ask another question on Unity Answers... thanks for the help, though