Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2016
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Java script worked 4 days ago. Now its not.

    I had this coding working to turn on and off a flash light with battery drain as well. Keep getting compile errors. No longer working in my game. Please help. Errors in image below.

    Code:
    #pragma strict
    
    var flashlightLightSource : Light;
    var lightOn : boolean = true;
    var lightDrain : float = 0.1;
    private static var batteryLife : float = 0.0;
    var maxBatteryLife : float = 2.0;
    
    private static var batteryPower : float = 1;
    
    var barDisplay : float = 0;
    var pos : Vector2 = new Vector2(20,40);
    var size : Vector2 = new Vector2(60,20);
    var progressBarEmpty : Texture2D;
    var progressBarFull : Texture2D;
    
    var soundTurnOn : AudioClip;
    var soundTurnOff : AudioClip;
    
    
    function Start()
    {
    	batteryLife = maxBatteryLife;
    	flashlightLightSource = GetComponent(Light);
    }
    
    
    static function AlterEnergy (amount : int)
    {
    	batteryLife = Mathf.Clamp(batteryLife+batteryPower, 0, 100);
    
    }
    
    
    
    function Update()
    {
    
    //BATTERY LIFE BRIGHTNESS//////////
    	if(lightOn && batteryLife >= 0)
    	{
    		batteryLife -= Time.deltaTime * lightDrain;
    	}
    		if(lightOn && batteryLife <= 0.4)
    	{
    		flashlightLightSource.light.intensity = 5;
    	}
    		if(lightOn && batteryLife <= 0.3)
    	{
    		flashlightLightSource.light.intensity = 4;
    	}
    	if(lightOn && batteryLife <= 0.2)
    	{
    		flashlightLightSource.light.intensity = 3;
    	}
    		if(lightOn && batteryLife <= 0.1)
    	{
    		flashlightLightSource.light.intensity = 2;
    	}
    			if(lightOn && batteryLife <= 0)
    	{
    		flashlightLightSource.light.intensity = 0;
    	}
    	
    
    	
    	barDisplay = batteryLife;
    	
    	if(batteryLife <= 0)
    	{
    		batteryLife = 0;
    		lightOn = false;
    	}
    	
    	if(Input.GetKeyUp(KeyCode.F))
    	{
    		toggleFlashlight();
    		toggleFlashlightSFX();
    		
    		if(lightOn)
    		{
    			lightOn = false;
    		}
    		else if (!lightOn && batteryLife >= 0)
    		{
    			lightOn = true;
    		}
    	}
    }
    	
    	/////// PIC  ///////////
    function OnGUI()
    {
     
        // draw the background:
        GUI.BeginGroup (new Rect (pos.x, pos.y, size.x, size.y));
            GUI.Box (Rect (0,0, size.x, size.y),progressBarEmpty);
     
            // draw the filled-in part:
            GUI.BeginGroup (new Rect (0, 0, size.x * barDisplay, size.y));
                GUI.Box (Rect (0,0, size.x, size.y),progressBarFull);
            GUI.EndGroup ();
     
        GUI.EndGroup ();
     
    } 
     	
    function toggleFlashlight()
    {
    	if(lightOn)
    	{
    		flashlightLightSource.enabled = false;
    	}
    	else
    	{
    		flashlightLightSource.enabled = true;
    	}
    }
    function toggleFlashlightSFX()
    {
    	if(flashlightLightSource.enabled)
    	{
    		audio.clip = soundTurnOn;
    	}
    	else
    	{
    		audio.clip = soundTurnOff;
    	}
    	audio.Play();
    	
    }
    
    	@script RequireComponent;(AudioSource)
    -capture-jpg

  2. #2
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,278
    Thanks
    21
    Thanked 548 Times in 542 Posts
    # is not a comment delimiter in JS.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  3. #3
    Administrator VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    10,676
    Thanks
    6
    Thanked 1,266 Times in 1,236 Posts
    I suppose the OP confused JavaScript with Java. Moved to the Java forum.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •