Search Unity

C4 script help

Discussion in 'Scripting' started by Bibbobbib, Sep 16, 2012.

  1. Bibbobbib

    Bibbobbib

    Joined:
    May 27, 2012
    Posts:
    11
    Hi, I need some help with a C4 script. It's not mine, its written by OcularCash of armedunity.com.
    here is the throw script.

    Code (javascript):
    1. var C4Prefab : Transform;
    2. var throwForce = 500;
    3. //public var DrawAudio : AudioClip;
    4. //public var ThrowAudio : AudioClip;
    5. //public var C4TriggerAudio : AudioClip;
    6. var isHolding = false;
    7. var CanDetonate = false;
    8. var DetonationSwitch = false;
    9.  
    10. function Update()
    11. {
    12.         if(Input.GetKeyDown("t")(CanDetonate == false))
    13.         {
    14.                 //audio.PlayOneShot(DrawAudio);
    15.                 //animation.Play("C4Draw");
    16.                 isHolding = true;
    17.         }
    18.         if(Input.GetKeyUp("t")(isHolding == true))
    19.         {
    20.                 //audio.PlayOneShot(ThrowAudio);
    21.                 var C4 = Instantiate(C4Prefab, transform.position, Quaternion.identity);
    22.                 C4.rigidbody.AddForce(transform.forward * throwForce);
    23.                 //animation.Play("C4Throw");
    24.                 isHolding = false;
    25.                 CanDetonate = true;
    26.         }
    27.         if(Input.GetKeyDown("t")(CanDetonate == true))
    28.         {
    29.                 DetonationSwitch = true;
    30.                 //animation.Play("PressC4Trigger");
    31.                 //audio.PlayOneShot(C4TriggerAudio);
    32.                 CanDetonate = false;
    33.         }
    34.         if(Input.GetKeyUp("t")(isHolding == false))
    35.         {
    36.                 DetonationSwitch = false;
    37.         }              
    38. }
    And here is the C4 script.

    Code (javascript):
    1. var ySpeed : float = 5;
    2. var zSpeed : float = 2.5;
    3. var RotateTilCollide = false;
    4. var explosionPrefab : Transform;
    5. //var HitWallAudio : AudioClip;
    6. var FindTrigger : C4Throw;
    7.  
    8. function Start()
    9. {
    10.         FindTrigger = GameObject.Find("SpawnPoint").GetComponent("C4Throw");
    11.         RotateTilCollide = true;
    12. }
    13.  
    14. function Update ()
    15. {
    16.         if(RotateTilCollide == true)
    17.         {
    18.                 transform.Rotate(0, 45 * ySpeed * Time.deltaTime, 90 * zSpeed * Time.deltaTime);
    19.         }
    20.         if(FindTrigger.DetonationSwitch == true)
    21.         {
    22.                 Destroy(gameObject);
    23.                 Instantiate(explosionPrefab, transform.position, Quaternion.identity);
    24.         }
    25. }
    26.  
    27. function OnCollisionEnter(col:Collision)
    28. {
    29.         RotateTilCollide = false;
    30.         //audio.PlayOneShot(HitWallAudio);
    31.         Destroy (rigidbody);
    32. }
    The problem is, is when i place a C4 (or in my case a cube for a test) it will stick to the floor as it should. But when i press t to detonate it just places another C4/cube. Also I am getting a NullReference error.

    This:
    Code (csharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. C4Script.Update () (at Assets/Resources/NewScripts/C4Script.js:20)
    Any solutions?
     
    Last edited: Sep 16, 2012
  2. ShadoX

    ShadoX

    Joined:
    Aug 25, 2010
    Posts:
    260
    I'm probably wrong, but is "gameObject" in the C4 script actually somewhere defined? Perhaps that's the reason for the Null..Exception.
     
  3. Bibbobbib

    Bibbobbib

    Joined:
    May 27, 2012
    Posts:
    11
    What do you mean??

    This?
    FindTrigger = GameObject.Find("SpawnPoint").GetComponent("C4Throw");
     
  4. ShadoX

    ShadoX

    Joined:
    Aug 25, 2010
    Posts:
    260
    Can't test it myself, so my next assumtion would be that either ^that line doesn't get the object you are looking for or that you should try something like...
    Destroy(FindTrigger.gameObject);
    or whatever else the right way is to get the gameObject for FindTrigger. (I'm just throwing out ideas so sorry, if the problem isn't in those two lines)
     
  5. Bibbobbib

    Bibbobbib

    Joined:
    May 27, 2012
    Posts:
    11
    When i double click the error in the compiler, it opens unitron and that line is highlighted. So that is the the line with the error. btw im not sure what happened with the space between o and w in throw.
     
  6. ShadoX

    ShadoX

    Joined:
    Aug 25, 2010
    Posts:
    260
    Not sure which line you are referring to, but you should probably make sure that it's doing what it is supposed to.
     
  7. Bibbobbib

    Bibbobbib

    Joined:
    May 27, 2012
    Posts:
    11
    I will see if I can get hold of the writ of the script. I think it's something to do with the detonation switch variable, i can't work out what it does.