- Home /
Component not being added with predefined public variables
I have written a script that adds an image effect to the main camera for 30 seconds before wearing off, at which point the image effect is removed and an AudioClip is played.
This would all work except the image effect is added without the shaders that I set up on the script inside the Inspector. This is also the case with the AudioClip which was also set up in the Inspector, when the below script is added from another script using AddComponent() the AudioClip is left out.
#pragma strict
@script AddComponentMenu("Abilities/Night Vision Effect")
private var duration : float = 30.0;
private var timer : float = 0.0;
private var nightVision : NightVision;
public var wearOff : AudioClip; //Defined in the Inspector...
function Start () {
timer = Time.time + duration;
//Add night vision and store a reference to call Destroy on later.
nightVision = camera.main.gameObject.AddComponent(NightVision);
}
function Update () {
if (Time.time > timer) {
//Play wear off sound if it was assigned in the inspector
if (wearOff != null) {
AudioSource.PlayClipAtPoint(wearOff, Vector3.zero, PlayerPrefs.GetFloat("PREF_audio_effects", 0.85));
}
//Destroy night vision component and this component
Destroy(nightVision);
Destroy(this);
}
}
I'm not understanding why the shaders aren't getting added with the image effect component as well as why the AudioClip is going missing, is there another method for doing such a thing?
Do you mean you setup default references on the script itself? Or where did you set the AudioClip. Also do you talke abour this
NightVision script? This one get's it's own shader when the script becomes active.
Default References are an editor feature. When you use AddComponent from a Script the default references aren't set. You need to create a prefab which is actually able to serialize such references as an asset.
Yeah, I was setting up the AudioClip and Shaders in the default references for each script. Using 'Shader.Find' to access the shaders for the image effect like in that NightVision script you mentioned seems to have fixed the issue of the missing shaders.