Light.intensity

Simple question, but the documentation only tackles sliding between intensity, and couldn’t find anything here or in google.

Before lightChange(), light intensity is at 8. I want to change it to 4 within the function. However I’ve noticed because my scripting is not correct, it blocks the rest of the code. What am I missing? I thought it would be much simpler than this…

Thanks in advance.

var pace : float;
var spotlight : Light;
var clock : float;
var changeTime : float;

function Start () {
    lightSwitch();
}

function Update () {
    clock += Time.deltaTime;
}

function ranNumGen() {
        var number = Random.value;
        return number;
}

function lightSwitch(){
    while(clock < changeTime){
        if(ranNumGen() <= 0.6){
            spotlight.enabled = true;
            Debug.Log("enabled at " + Time.time + "seconds");
        } else {
            spotlight.enabled = false;
            Debug.Log("disabled at " + Time.time + "seconds");
        }
        yield WaitForSeconds(pace);
    }
    Debug.Log("change time");
    lightChange();
}

function lightChange(){
    Debug.Log("activated");
    spotlight.color = Color(0, 0, 255);
    spotlight.intensity = 4;
    spotlight.transform.position = Vector3(0, 60, 0);
    spotlight.transform.rotation = Quaternion.Euler(90, 0, 0);
}

That’s a lot of code to read and then guess what it is that’s wrong with it and how to solve it for you since you never explained. What does it mean: “scripting is not correct” and “it blocks the rest of the code”?

It will help if you explain what it is you’re trying to do and what is happening instead exactly.

Well considering I’m not getting the desired result, I assume it’s because I’m not writing something correctly.

As said, I want to change the light intensity within the function lightChange from 8 (default) to a new setting of 4. I thought it could be done with spotlight.intensity = 4;, but the intensity (or light settings for that matter) doesn’t change. Blocking out the spotlight.intensity code with // allows the rest of the code to function; leaving it in only results in the Debug.Log call getting through.

Apologies for not truncating the code.

So what you’re saying is that when:

function lightChange(){
  Debug.Log("activated");
  spotlight.color = Color(0, 0, 255);
  spotlight.intensity = 4;
  spotlight.transform.position = Vector3(0, 60, 0);
  spotlight.transform.rotation = Quaternion.Euler(90, 0, 0);
}

then first line of lightChange gets exectued but nothing after it does and when:

function lightChange(){
  Debug.Log("activated");
  spotlight.color = Color(0, 0, 255);
  //spotlight.intensity = 4;
  spotlight.transform.position = Vector3(0, 60, 0);
  spotlight.transform.rotation = Quaternion.Euler(90, 0, 0);
}

then everything gets executed? I don’t see how would that be possible.

I tested your script and no such thing happens. Tried:

function lightChange(){
  Debug.Log("activated_1");
  spotlight.color = Color(0, 0, 255);
  Debug.Log("activated_2");
  spotlight.intensity = 4;
  Debug.Log("activated_3");
  spotlight.transform.position = Vector3(0, 60, 0);
  Debug.Log("activated_4");
  spotlight.transform.rotation = Quaternion.Euler(90, 0, 0);
  Debug.Log("activated_5");
}

All Debug.Log-s get executed as well as all the rest of the code works just fine and commenting out the intensity line has no effect on it nor ever would it.

You need to do some more testing and nerrow down you problem and state it precisly because what you claimed is happening (“it blocks the rest of the code”) just isn’t happening.

1 Like

Thank you for the testing practice, I didn’t think of that kind of style.
Will do some more testing generally though and get back to you.