- Home /
adjusting detonator variables with javascript
Hi - I'm new to unity. Anyone have a good example of how to adjust the detonator unity extension variables with javascript? I just have a basic example set up - sphere falls on plane and explodes.
Sphere has a script that instantiates the explosion on collision:
var explosion : Transform;
function OnCollisionEnter () {
Destroy (gameObject);
var explosionInstance : Transform;
explosionInstance = Instantiate(explosion,transform.position, transform.rotation);
}
Explosion is a prefab which contains the detonator-simple prefab. I want to be able to modify the predefined variables of the Detonator script like Duration,Detail,DestroyTime,Size. The simplest example should do fine. Thanks!
Answer by aldonaletto · Jul 14, 2011 at 05:41 PM
EDITED: I adapted the answer to your script. Now it will find a script called Detonator attached to the explosion clone and let a reference in scriptRef, which you will use to modify the Detonator variables. It should work, but you may have problems if some of these variables are used in the Awake function of Detonator.js - as far as I know, Awake is executed during instantiation, thus before your script can modify anything (I believe Start will be executed only right before the next frame, so the modifications will take effect).
var explosion : Transform;
function OnCollisionEnter () {
Destroy (gameObject);
var explosionInstance : Transform;
explosionInstance = Instantiate(explosion,transform.position, transform.rotation);
var scriptRef: Detonator = explosionInstance.GetComponent(Detonator);
scriptRef.Duration = ...; // modify variables using scriptRef
}
Yes - I can modify the values in the inspector, and I can see the original script for the prefab (Detonator.js) - but I don't want to modify the original script, I just want to modify instances of objects which have the prefab (the script) attached to them. For example, if a sphere falls to the ground that is large, I want the Size value of the explosion to be larger. If the sphere is small, I want to create a small explosion.
This is detonator by the way. Very fun. http://unity3d.com/support/resources/unity-extensions/explosion-framework.html
I have already edited my answer to include the modification of variables by script - but right after instantiation. Is it what you're looking for?
Hmmmm, possibly, but I don't think so. I'm not the greatest coder, so it helps me to keep things relevant to my examples. I get what you're saying, but I'm not really instantiating the prefab the way you are above. In my example - Explosion is the prefab from the Detonator framework which contains the Detonator script (and all of its adjustable variables). Sphere is a game object which has been attached the above script. In the inspector, under explosion variable, I have dragged the Explosion prefab. However, it does not work for me to say explosion.Detonator.Size = 1000 - because it is not a member of Unity.Transform. On the other hand, if I instantiate Explosion as a game object, which I think is what you're trying to do above, and change thing, how would I then get my code above to reference this newly modified instance of Explosion?
And how do you create the Sphere object? By hand, in the Editor? Or have you created a Sphere prefab, and is instantiating Sphere clones in your script when you need?
Your answer
![](https://koobas.hobune.stream/wayback/20220612143546im_/https://answers.unity.com/themes/thub/images/avi.jpg)