Programming a gun to shoot.

Ok so I keep getting an error that says:

An element with the same key already exists in the dictionary. (Error: System.ArgumentException)

Here are the two scripts I’m using:

#pragmastrict

varrocket : Rigidbody; //ourprefab
varthrowPower : float = 10; //powervariabledetermineshowfastthisobjectwillbe “shotout”
varsound : AudioClip; //canonfiresoundClip

varcanShoot = 1;
varshootDelay = 0;

functionStart ()
{
Cursor.visible = false;
}

functionUpdate () {

if (Input.GetButtonDown(“Fire1”)) {
if (canShoot == 1) {
varclone : Rigidbody; //variablewithinavariable
clone = Instantiate(rocket, transform.position, transform.rotation); //theclonevariableholdsourinstantiateaction
clone.velocity = transform.TransformDirection(Vector3.forward * throwPower); //appliesforcetoourprefabusingthe “forward” positiontimesourthrowPowervariable
AudioSource.PlayClipAtPoint(sound, transform.position, 1); //object’sZaxis
canShoot = 0;
}
}

if (canShoot == 0) {
if (shootDelay < 30) {
shootDelay == shootDelay+1;
}
else {
shootDelay == 0;
canShoot == 1;
}
}

}

and this:

#pragmastrict

varradius : float = 5.0; //providesaradiusatwhichtheexplosivewilleffectrigidbodies
varpower : float = 10.0; //providesexplosivepower
varexplosiveLift : float = 1.0; //determineshowtheexplosionreacts. Ahighervaluemeansrigidbodieswillflyupward

functionStart () {

}

functionOnCollisionEnter (collision : Collision) {
vargrenadeOrigin : Vector3 = transform.position;
varcolliders : Collider[ ] = Physics.OverlapSphere (grenadeOrigin, radius); //thisissayingthatifanycolliderwithintheradiusofourobjectwillfeeltheexplosion
for(varhit : Colliderincolliders){ //forloopthatsaysifwehitanycolliders, thendothefollowingbelow
if (hit.GetComponent.()){
hit.GetComponent.().AddExplosionForce(power, grenadeOrigin, radius, explosiveLift); //ifwehitanyrigidbodiesthenaddforcebasedoffourpower, thepositionoftheexplosionobject
//Destroy(gameObject); //theradiusandfinallytheexplosivelift. Afterwardsdestroythegameobject
}

}

}

Use Code Tags please.

I’d help, but I haven’t gotten into dictionaries or JS yet so…

I don’t think the problem is in this code. I don’t see any assignment to a dictionary here but the code is pretty brutal to read without proper formatting.

1 Like