Start getting called twice

I'm using the simple damage script from the FPS tutorial in my game. For some reason though it is being called twice. Below is my code anyone have a clue what I am doing wrong?

Code for creating explosion

var explosionTime = 1.0;
var explosionRadius = 5.0;
var explosionPower = 2000.0;
var explosionDamage = 80.0;

function Start()
{
    Debug.Log("Start Called");
    var explosionPosition = transform.position;
    var colliders : Collider[] = Physics.OverlapSphere (explosionPosition,explosionRadius);

    for (var hit in colliders)
    {
        if (!hit) continue;


        if (hit.rigidbody)
        {
            Debug.Log("hit");
            Debug.Log(hit.rigidbody.name);


            hit.rigidbody.AddExplosionForce(explosionPower,explosionPosition, explosionRadius, 3.0);
            var closestPoint = hit.rigidbody.ClosestPointOnBounds(explosionPosition);
            var distance = Vector3.Distance(closestPoint, explosionPosition);

            // The hit points we apply fall decrease with distance from the hit point
            var hitPoints = 1.0 - Mathf.Clamp01(distance / explosionRadius);
            hitPoints *= explosionDamage;

            // Tell the rigidbody or any other script attached to the hit object
            // how much damage is to be applied!
            hit.rigidbody.SendMessageUpwards("ApplyDamage", hitPoints, SendMessageOptions.DontRequireReceiver);

        }
    }
    // stop emitting ?
    if (particleEmitter)
    {
        particleEmitter.emit = true;
        yield WaitForSeconds(0.5);
        particleEmitter.emit = false;
    }
    // destroy the explosion
    Destroy (gameObject, explosionTime);
}

Here is the code that creates the explosion

function Detonate()
{
var instantiatedExplosion : GameObject = Instantiate(explosion, transform.position, transform.rotation );

Destroy( gameObject );
}

Damage is being applied to all my objects twice, and debug is showing "Start Called" twice.

I didn't bother to read the rest of it, because "Start Called" being output twice is the root of the problem. Are you sure you haven't added the script to the Game Object twice?

2 Likes

After scanning your code I think Jessy is probably right. The script must be getting called twice.

Thanks, you just made be feel like an idiot, my prefab had the script twice. problem solved :sweat_smile: Thanks

On another note, when you Instantiate an object, is it possible to send the start, or some other function, parameters?

[quote]
On another note, when you Instantiate an object, is it possible to send the start, or some other function, parameters?
[/quote]

I would think you should not be able to. But I haven't tried it, because it isn't necessary. Just make a new function and call it from start then you can call it from every script.

Like

function Start()
{
NewFunction();
}

function NewFunction()
{
doStuff;
}

What I want to do is pass the new object some parameters what I currently do is:

    var instantiatedExplosion : GameObject = Instantiate(explosion, transform.position, transform.rotation );
    var explosion = instantiatedExplosion.GetComponent(Explosion);//Get our explosion script and setup settings
    explosion.explosionTime = explosionTime;
    explosion.explosionRadius = explosionRadius;
    explosion.explosionPower = explosionPower;
    explosion.explosionDamage = explosionDamage;
    explosion.Explode();
    Destroy( gameObject );

I would like to do something like:

var instantiatedExplosion : GameObject = Instantiate(explosion(explosionTime,explosionRadius,explosionPower), transform.position, transform.rotation );

The way I'm doing it just seems impractical.

Edit:
Seems this has been asked before http://forum.unity3d.com/viewtopic.php?t=42039&highlight=passing+parameters+instantiate, and isn't possible. Seems like this would be a good option to have.

[quote]
What I want to do is pass the new object some parameters what I currently do is:

Code:

var instantiatedExplosion : GameObject = Instantiate(explosion, transform.position, transform.rotation );
var explosion = instantiatedExplosion.GetComponent(Explosion);//Get our explosion script and setup settings
explosion.explosionTime = explosionTime;
explosion.explosionRadius = explosionRadius;
explosion.explosionPower = explosionPower;
explosion.explosionDamage = explosionDamage;
explosion.Explode();
Destroy( gameObject );

I would like to do something like:
Code:

var instantiatedExplosion : GameObject = Instantiate(explosion(explosionTime,explosionRadius,explosionPower), transform.position, transform.rotation );

The way I'm doing it just seems impractical.
[/quote]

Nope but you could setup up a function to get the parameters.

var instantiatedExplosion : GameObject = Instantiate(explosion, transform.position, transform.rotation );
   var explosion = instantiatedExplosion.GetComponent(Explosion);//Get our explosion script and setup settings
   explosion.Explosion(time,radius,power,damage);
   explosion.Explode();
   Destroy( gameObject );

function explosion(eTime, eRad, ePow, eDam)
{
time= eTime;
rad=eRad;
power=ePow;
damage=eDam;
}

Hi all,

I also had the problem of double Start method call.
I feel lonely when I saw I loaded in an asynchronous way my scene twice...

The problem is always between the keyboard and the chair :)

2 Likes

Holy necropost Batman!

1 Like

Please don't necro threads, especially for this kind of comment.

Community Code of Conduct

Thanks.