- Home /
JS Script only work in Editor
So i have two script using the same Line Renderer, only that one of them will only work in the Editor while i do testing. This Script is the one that will only work in the Editor and when i compile it as a webplayer it stop render the line but the second script still work and they more or less do the same thing.
var lazerSound : AudioClip;
var damage = 5;
var distance = 150.0;
//var debug = false;
function Start () {
tmpInWidth = maxWidth;
}
private var width = 0.0;
private var charge = false;
private var tParamCharge : float = 0;
private var tParamDeCharge : float = 0;
private var tParamPing : float = 0;
private var tParamPong : float = 0;
private var pingpong = false;
private var ping = false;
private var tmpDesWidth = 0.0;
private var tmpInWidth = 0.5;
var speed : float = 0.1;
var bounce : float = 0.1;
var maxBonce = 0.6;
var minBonce = 0.4;
var maxWidth = 0.5;
function Update () {
if(global.team == "Human"){
var line : LineRenderer = gameObject.GetComponent(LineRenderer);
var hit : RaycastHit;
if (Physics.Raycast(transform.position, transform.TransformDirection (Vector3.forward), hit, Mathf.Infinity)) {
if(!Input.GetMouseButton(1)){
line.SetPosition(1, hit.point);
line.SetPosition(0, transform.position);
}
//Debug.DrawRay(transform.position, transform.forward*distance, Color.red);
if (Input.GetMouseButton(0) && !Input.GetMouseButton(1) && global.energy > 0) {
global.energy--;
PlayClipAt(lazerSound, transform.position);
//line.enabled = true;
tParamDeCharge = 0.0;
if(!pingpong){
tParamCharge += Time.deltaTime * speed;
width = Mathf.Lerp(0, tmpInWidth, tParamCharge);
if(width == 0.5)
pingpong = true;
}
else{
if(ping){
tParamPing += Time.deltaTime * bounce;
tParamPong = 0.0;
width = Mathf.Lerp(minBonce, maxBonce, tParamPing);
if(width == maxBonce){
ping = false;
}
}
else{
tParamPong += Time.deltaTime * bounce;
tParamPing = 0.0;
width = Mathf.Lerp(minBonce, maxBonce, tParamPong);
if(width == maxBonce){
ping = true;
}
}
}
tmpDesWidth = width;
global.debug(""+width);
line.SetWidth(width,width);
if(hit.transform.tag == "Alien"){
hit.transform.GetComponent(ai).life--;
global.debug(""+hit.transform.GetComponent(ai).life);
}
//global.debug("Pew pew");
}
else{
if(width == 0){
//line.enabled = false;
tParamCharge = 0.0;
tParamDeCharge = 0.0;
tmpDesWidth = 0.0;
tmpInWidth = maxWidth;
pingpong = false;
}
else{
tParamDeCharge += Time.deltaTime * speed;
tParamCharge = 0.0;
width = Mathf.Lerp(tmpDesWidth, 0, tParamDeCharge);
tmpInWidth = width;
line.SetWidth(width,width);
}
}
}
}
}
function PlayClipAt(clip: AudioClip, pos: Vector3) {
var tempGO = GameObject("TempAudio"); // create the temp object
tempGO.transform.position = pos; // set its position
tempGO.transform.parent = transform;
var aSource = tempGO.AddComponent(AudioSource); // add an audio source
aSource.clip = clip; // define the clip
aSource.minDistance = 15.0;
aSource.volume = 0.05;
// set other aSource properties here, if desired
aSource.Play(); // start the sound
Destroy(tempGO, clip.length); // destroy object after clip duration
return aSource; // return the AudioSource reference
}
While this script do work!
var scanBaseTime = 10.0;
var scanTick = 0.4;
var scanDistance = 3.0;
var scanTexture:Texture2D;
private var scanTime:float;
private var currScanning:Transform;
private var scanning = false;
private var line : LineRenderer;
var durationEffect : float = 1.0;
function Update () {
scan();
}
function scan(){
if(global.team == "Human"){
line = gameObject.GetComponent(LineRenderer);
if (Input.GetMouseButton(1) && !Input.GetMouseButton(0) && global.energy > 0) {
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast(transform.position, transform.TransformDirection (Vector3.forward), hit, scanDistance)) {
if (hit.transform.tag == "scan"){
if(currScanning != null){
if(!scanning){
for (var i = 0; i < global.scanned.length; i++) {
if(global.scanned[i].name == currScanning.name){
reset();
return;
}
}
scanning = true;
}
else{
if (scanTime > 0){
if(!whaitEnergyGenerate)
generateEnergy();
scanTime -= Time.deltaTime;
//line.enabled = true;
var lerp : float = Mathf.PingPong (Time.time, durationEffect) / durationEffect;
var extra = Mathf.Lerp (-0.5, 0.5, lerp);
line.SetPosition(0, transform.position);
line.SetPosition(1, hit.point+(transform.up*extra));
line.SetWidth(0.5,5);
Debug.Log("Scanning: "+hit.transform.name);
}
else{
Debug.Log("Scanned: "+hit.transform.name);
global.scanned.Push(hit.transform);
reset();
return;
}
}
}
else{
currScanning = hit.transform;
}
}
else{
reset();
}
}
else{
reset();
}
}
else{
reset();
}
}
}
private var whaitEnergyGenerate = false;
function generateEnergy(){
whaitEnergyGenerate = true;
yield WaitForSeconds (scanTick);
global.energy--;
Debug.Log(global.energy);
whaitEnergyGenerate = false;
}
function reset(){
scanTime = scanBaseTime;
currScanning = null;
scanning = false;
line.SetWidth(0,0);
//line.enabled = false;
}
function OnGUI () {
if(scanning){
GUI.BeginGroup (new Rect (Screen.width / 2 - 100, Screen.height / 2 - 10, 200*(scanTime/scanBaseTime), 20));
GUI.Label(new Rect (0,0,200,20), scanTexture);
GUI.EndGroup ();
GUI.Label(new Rect (Screen.width / 2 - 100, Screen.height / 2 - 10,200,20), "Time: "+ Mathf.CeilToInt(scanTime));
}
}
Tried my best to format this, sorry i fail'd.
That is a little too long to read. Consider isolating what is relevant, that would speed up the help.
Also, provide a description of the problem, you tell what is here but what is wrong now?
The problem is that i have no clue why it only work in the Unity 3D Engine and when i compile it, the only thing that do not function is the first script. I have been looking at it for hours and i cant figure out why it wont work. It plays the sound and drain energy, set positions and change the width. However the renderer wont show in the first script even tho i have done the exact same thing in the second script.
So shorten the scripts to what when i do not know what to short it to :/