Sending Damage amount to instantiate object sends previous variable

I have a charge shot for my player character in which it instantiates a prefab with a script containing

var appliedDamage : float;

and a couple of other functions which don't effect the variable at all!

In the firing script for my player I have this function:-

    if (Input.GetButton("ChargeFire"))
    {
        StopFunction = true;
        particleEmitter.emit = false;
        if (CanCharge == true)
        {
            Ammo = Ammo - 30*Time.deltaTime;
            ChargeSize = ChargeSize + 1*Time.deltaTime;
            CSamount = CSamount + 25*Time.deltaTime;
        }
        if (Ammo <=1)
        {
            Hold();
            CSamount = 90;
        }   
    }
    if (Input.GetButtonUp("ChargeFire"))
    {
    //SetDamageAmount();
    ChargeShot();
    if (Ammo <=2)
    {   
        StopFunction = false;
        Reloading();
    }
    else
    {
        StopFunction = false;
    }
}
    function ChargeShot()
    {
        var chargedshot = Instantiate(ChargeTea, transform.position, transform.rotation);
        chargedshot.rigidbody.velocity = transform.forward*5 + transform.up*5;
        chargedshot.transform.localScale = Vector3 (ChargeSize/20,ChargeSize/20,ChargeSize/20);
        yield WaitForSeconds (0.1);
        var senddamage = ChargeTea.GetComponent (ChargeShotScript);
        senddamage.appliedDamage = CSamount;
        ChargeSize = 0;
        CanCharge = true;
        yield WaitForSeconds (0.2);
        CSamount = 0;
    }

I have all the variable set just havn't incldued them to save space. My problem is that in the ChargeShot function, where it sends the damage accumulated during Input.GetButtonDown, my 1st shot doesn't set any damage. Every shot after that sets the damage amount on the prefab clone to that of the previous shot. Obviously I don't want it to do this (hence why it's a problem lol), but I may be scripting it wrong.

Could anybody please shed some light on what I may be doing wrong?

edit Sorry I commpletely forgot to include the input.GetButtonUp part of my code which is how I call the ChargeShot function. Cheers for your patience.

How are you calling your ChargShot function?

–

Not 100%. I see you've added a similar answer without it. So I guess not?

–

Apparently the following line is not necessary (line 7 in my original answer above): entry.callback = new EventTrigger.TriggerEvent();

–

2 Answers

2

These lines, in chargedShot (your firing function):

var chargedshot = Instantiate(ChargeTea, ... // chargedShot is my new bullet
   ...
var senddamage = ChargeTea.GetComponent (ChargeShotScript);
                   ^^^^--gets script of the PREFAB!!
senddamage.appliedDamage = CSamount; // changes damage of all future shots!!

Using ChargeTea is setting the damage of everything you spawn from now on. I'm kind of surprised it's even legal to change a prefab like that. You probably wanted to set the damage of the thing you just spawned:

var senddamage = chargedshot.GetComponent (ChargeShotScript);
                  ^^^^^--the shot I just created

You sir are a f@?>ing legend!!!!!! Cheers it's been driving me crazy for 3 days, I just didn't have a clue what was going wrong hence why I put the yield statement, of course I wanted to change the value on the single prefab clone not the entire prefab itself, you have shown me the light and I am greatful lol

–

"sets the damage ... to that of the previous" really narrowed it down. A lot of posters would have stopped testing at "doesn't work" or "gives random results." More than half the work of debugging is narrowing down the problem.

–

Hey man, where are you calling ChargeShot()?

in the Get.ButtonUp function... have edited the code to include it lol, totally forgot before, my bad.

–