Variable Access Problem (Javascript)

/I USE JAVASCRIPT\

Hey, I’m working on a little practice project. I’m trying to make a remote explosive, basically a C4 charge. I have gotten the C4 prefab to instantiate, but I need a way to tell the C4 charge that I spawned to destroy itself and instantiate an explosion in it’s place.

I’ve tried a GetComponent but the whole concept is confusing and it just seems like people are just pretending to know what they’re talking about because half of what they type is usually copied from the Unity script references, and those are a little vague and incomprehensible on the subject.

I’ve tried making a function on the C4’s trigger that adds 1 to a variable (which is at 0) and then checking if the variable is over 0 on the C4 Charge then trying to destroy the Charge while instantiating an explosion. But I just can’t figure out how to get the variable.

I have everything for the C4 working(animations, instantiating the charge) except for the part on how to access the variable and trigger the explosion.

Please help, I can’t find the answers on Google and I don’t completely understand the Unity Script References for the GetComponent function.

On your player:

var C4 : GameObject;

function Update(){
    if (Input.GetButtonDown("Fire1")){
        C4.SendMessage("Explode");
    }
}

On your C4:

function Explode(){
//instantiate your explosion
//destroy your C4
}

The trigger must be part of the C4 charge, and you can make the C4 explode in its script’s OnTriggerEnter. Attach this script to the C4 prefab, then drag the explosion prefab to the variable explosion in the Inspector:

var explosion: GameObject; // drag the explosion here

function OnTriggerEnter(col:Collider){

  if (col.tag == "Player"){ // only the Player explodes it
    //instantiate the explosion
    Instantiate(explosion, transform.position, Quaternion.identity);
    Destroy(gameObject); // suicide this C4 object
  }
}

I’m supposing that:

  • the trigger is part of the C4 prefab;

  • the player has the tag “Player”;

If you have everything else set up and just need to tell the C4 script that it needs to explode then GetComponent will work fine.

On your button / plunger / trigger script:

var buttonPressed : boolean;

function Update() {
    // I'm using pressing the spacebar as the trigger as an example
    if (Input.GetKeyDown("space")) {
        buttonPressed = true;
    }

    if (buttonPressed) {
        // We're saying find the GameObject called c4 and get the attached script C4Script
        // Quotes aren't necessary around C4Script as it's a type
        var c4Script : C4Script = gameObject.Find("c4").GetComponent(C4Script);

        // Now you can access all public variables and functions contained in that script
        c4Script.explodeC4(); // This calls your explode function on the C4

        buttonPressed = false; // Set this to false to stop explodeC4 being constantly called
    }
}

Then on your c4 script you can create your explode function:

function explodeC4() {
    // Instantiate the explosion effect and destroy the GameObject
}