daylight cycle script crashing unity

Updated 6 days ago in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on December 3, 2017

my daylight cycle is crashing unity whenever it runs (not so much crashing as permanently freezing) if anyone could point out why it would be awesome

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DayCycle : MonoBehaviour {

public Light lt;
public int daytime = 5;
public int nighttime = 5;
private bool Up;
private bool Down;
private float value;
public int time;
private bool running;

// Use this for initialization
void Start()
{
n2d();
lt = GetComponent<Light>();
lt.intensity = 1;
}

// Update is called once per frame
void Update()
{

}

private void FixedUpdate()
{
if (Up == true)
{
value = value + 0.05f;
lt.intensity = value;
}
if (Down == true)
{
value = value – 0.05f;
lt.intensity = value;
}
}
IEnumerator day()
{
Debug.Log(“its daytime”);
time = 1;
yield return new WaitForSeconds(daytime);
d2n();
}
IEnumerator night()
{
Debug.Log(“its nighttime”);
time = 3;
yield return new WaitForSeconds(nighttime);
n2d();
}
void n2d()
{
Debug.Log(“its dawn”);
time = 0;
running = true;
while (running)
{
Up = true;
if (value >= 1f)
{
Up = false;
value = 1;
lt.intensity = value;
running = false;
}
}
StartCoroutine(day());
}
void d2n()
{
Debug.Log(“its dusk”);
time = 2;
running = true;
while (running)
{
Down = true;
if (value <= 0f)
{
Down = false;
value = 0;
lt.intensity = value;
running = false;
}
}
StartCoroutine(night());
}
}

  • Liked by
Reply

The only problem I could find was that in the Start() function you call the n2d() function before you GetComponent<Light>() and store it to It variable.. So the n2d() function is trying to use the It variable, but as the It variable is null, it is not able to do anything and unty crashes..

  • Liked by
Reply
Cancel