Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

C# Help

Discussion in 'Scripting' started by Jsmucker, Oct 20, 2014.

  1. Jsmucker

    Jsmucker

    Joined:
    Sep 5, 2014
    Posts:
    48
    I need help locating the termination function in this part of a script. Instead of deleting an object, I wan't it to play an animation instead. Anyone know how to do this?

    Code (CSharp):
    1.    
    2. ///<summary>
    3. ///reducescurrenthealthby'damage'pointsandkillstheobjectifhealthrunsout
    4. ///</summary>
    5. publicvirtualvoidDamage(floatdamage)
    6. {
    7.  
    8. if (!enabled)
    9. return;
    10.  
    11. if (!vp_Utility.IsActive(gameObject))
    12. return;
    13.  
    14. if (m_CurrentHealth <= 0.0f)
    15. return;
    16.  
    17. m_CurrentHealth = Mathf.Min(m_CurrentHealth - damage, MaxHealth);
    18.  
    19. if (m_CurrentHealth <= 0.0f)
    20. vp_Timer.In(UnityEngine.Rand
    21. {
    22. SendMessage("Die"); //pickedupbyvp_DamageHandlersandvp_Respawners
    23. });
    24.  
    25.  
    26. //TIP: ifyouwanttodothingslikeplayaspecialimpact
    27. //sounduponeveryhit (butonlyiftheobjectsurvives)
    28. //thisistheplace
    29.  
    30. }
    31.  
    32.  
    33. ///<summary>
    34. ///removestheobject, playsthedeatheffectandschedules
    35. ///arespawnifenabled, otherwisedestroystheobject
    36. ///</summary>
    37. publicvirtualvoidDie()
    38. {
    39.  
    40. if (!enabled || !vp_Utility.IsActive(gameObject))
    41. return;
    42.  
    43. if (m_Audio != null)
    44. {
    45. m_Audio.pitch = Time.timeScale;
    46. m_Audio.PlayOneShot(DeathSound);
    47. }
    48.  
    49. RemoveBulletHoles();
    50.  
    51. vp_Utility.Activate(gameObject, false);
    52.  
    53. foreach (GameObjectoinDeathSpawnObjects)
    54. {
    55. if(o != null)
    56. vp_Utility.Instantiate(o, transform.position, transform.rotation);
    57. }
    58.  
    59. }
    60.      
    61.     }
     
    Last edited: Oct 20, 2014
  2. Cpt Chuckles

    Cpt Chuckles

    Joined:
    Dec 31, 2012
    Posts:
    86
    looks like the "termination" is on line 15
    Code (csharp):
    1. vp_Utility.Activate(gameObject, false);
     
  3. Jsmucker

    Jsmucker

    Joined:
    Sep 5, 2014
    Posts:
    48
    Sorry, I don't quite understand line 15 says "return". I have no idea how coding works in c# as I usually do it in javascript. Basically, I plan on simply deleting the termination code and substituting it for an animation instead.
     
  4. DylanYasen

    DylanYasen

    Joined:
    Oct 9, 2013
    Posts:
    50
    First of all, I believe Cpt Chuckles wanted to say on line 51. And I agree with him, the "termination" code looks like is in the "vp_Utility"'s "Activate" method. We don't have the code of "vp_Utility", so you may want to go check it yourself.

    Secondly, for the "return" you are asking about, it is just a way to skip the code after the "return",
    for example, on line 8 it means: if this script is not enabled, then return, which will skip the code after "return" in this entire method.
    It's being used here as a "guard". if condition is not satisfied, skip the following code.

    If you want to know the what's happening behind "return", this is not language specified thing, you may want to look for general programming&cs related materials.
     
    Jsmucker likes this.
  5. Jsmucker

    Jsmucker

    Joined:
    Sep 5, 2014
    Posts:
    48
    Awesome, Thanks!
    One more thing, How would I destroy a gameobject that is parented to the object that has this script?
     
  6. DylanYasen

    DylanYasen

    Joined:
    Oct 9, 2013
    Posts:
    50
    if you want to destroy(or do anything you want) a child's parent,
    you can access a gameobject's parent by
    transform.parent.gameobject

    then, do your thing. (keep it nasty)
     
  7. Jsmucker

    Jsmucker

    Joined:
    Sep 5, 2014
    Posts:
    48
    Ahh, great and how to access a gameobject's child?
     
  8. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
  9. Jsmucker

    Jsmucker

    Joined:
    Sep 5, 2014
    Posts:
    48
    So I entered the code below and got an error. Am I doing it right?
    Destroy(transform.GetChild.Cam2);
     
  10. Jsmucker

    Jsmucker

    Joined:
    Sep 5, 2014
    Posts:
    48
    It gives these three errors:
    Expression denotes a `method group', where a `variable', `value' or `type' was expected
    The best overloaded method match for `UnityEngine.Object.Destroy(UnityEngine.Object)' has some invalid arguments
    Argument `#1' cannot convert `object' expression to type `UnityEngine.Object'
     
  11. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    transform.GetChild (Cam2) works if Cam2 is an int (the index).
    You can use transform.Find("nameOfChild") instead.