Search Unity

var transfer seems to not working properly

Discussion in 'Scripting' started by gooongalooo, Dec 6, 2010.

  1. gooongalooo

    gooongalooo

    Joined:
    May 6, 2010
    Posts:
    140
    hi guys! maybe this is a simple problem with a simple solution, but i don´t understand the reason for the problem...but without a reason i not sure how to fix it;)
    what i want to do is to destroy a laser/projectile after it transfers it´s damage value...
    it seems sometimes not working properly... it get destroyed but the value don´t seems to be transferedbecause the object could be already destroyed...maybe...dunno.


    the cold logic: laser--> hit object--> make sure that it applies dmg ---> destroy laser

    well, after a laser beam is instatiated it generates a value
    (laser object is tagged with playerlaser)
    Code (csharp):
    1.  
    2. private var lifetime:float = 5;
    3. private var MinDmg:int = 1.0 ;
    4. private var MaxDmg:int = 4.0;
    5. private var critFactor:int = 2;
    6. private var LaserDmg:int;
    7.  
    8. function Start(){  
    9.     Destroy(gameObject,lifetime);
    10.     random = Random.Range(1,100);
    11.     if (random <= 20){
    12.         LaserDmg = Mathf.RoundToInt(Random.Range(MinDmg,MaxDmg))*critFactor;
    13.     }
    14.     else{
    15.         LaserDmg = Mathf.RoundToInt(Random.Range(MinDmg,MaxDmg));
    16.     }
    17. }
    18. function OnTriggerEnter (hit : Collider) {
    19.     if(hit.gameObject.tag == "EnemyDmgCollider"){
    20.         hit.SendMessage("SendLaserDmgToTarget", LaserDmg); // SEND DMG!!!
    21.         //Destroy(gameObject);
    22.     }
    23. }
    24. function DestroyLaser(){ // is called back in the enemy colliders
    25.     Destroy(gameObject);
    26. }
    so it sends it´s damage value to the "hit" object and there:

    Code (csharp):
    1. var EnemyCal:GameObject;
    2. var EnemyController:GameObject;
    3.  
    4. private var linked;
    5. private var linked2;
    6.  
    7. function Start(){
    8.     linked = EnemyCal.GetComponent("Enemy_GUI_EnerCal");
    9.     linked2 = EnemyController.GetComponent("Enemy_Controller");
    10. }
    11. function SendLaserDmgToTarget(LaserDmg : float){
    12.     if (linked.currShield > 0){
    13.         linked.SendMessage("SubtractLaserDmgFromShield", LaserDmg);
    14.         linked2.chasingTimer = true;
    15.         linked2.disableChasingTimer = 0;
    16.         }
    17.     if (linked.currShield <= 0   linked.currEnergy > 0){
    18.         linked.SendMessage("SubtractLaserDmgFromEnergy", LaserDmg);
    19.         linked2.chasingTimer = true;
    20.         linked2.disableChasingTimer = 0;
    21.         }
    22. }
    23. function OnTriggerEnter (hit : Collider){
    24.     if(hit.gameObject.tag == "playerlaser"){
    25.         hit.SendMessage("DestroyLaser");
    26.     }
    27. }
    28.  
    colliders now send the laser damage value to the script which calculates the stats of the enemy and
    after the laser hit the colliders it calls back the DestroyLaser if the laser beam.
    here is the function wich is called in the energy calculaion:
    Code (csharp):
    1. function SubtractLaserDmgFromShield(LaserDmg : int){
    2.     currShield -= LaserDmg;
    3. }
    4. function SubtractLaserDmgFromEnergy(LaserDmg: int){
    5.     currEnergy -= LaserDmg;
    6.  
    7. function Update(){
    8. //....  short verion ...bla
    9. if (currEnergy <= MaxEnergy){
    10.             currEnergy += (Time.deltaTime*BarRegenerationFactor) ;
    11.     }
    12. // this part works!!
    13.  
    14. }
    15. }
    !!! there are 2 laser initiated! ...it is posiible that there´s is problem calling the SubtractLaserDmgFromEnergy twice in the same time? well if i´m not standing perpendicular to the object one of the laser beams MUST be hit the object before the second laser beam will hit it ..so the function can´t be called twice at the same time...understand?^^

    so why it´s possible that the object gets destroyed without transfers it´s dmg var?
     
  2. gooongalooo

    gooongalooo

    Joined:
    May 6, 2010
    Posts:
    140
    is it possible that the colliders are guilty for that? do they need a min size to work properly? i did some debug.logs...IF the laser really hits, each single steps works correct...