i try to implement fade in and fade out in an object in duration loop with use thread.....but after play the game i get
NullReferenceException: Object reference not set to an instance of an object
error for this line
resetevent.Set ();
please help me.....
my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Threading;
using System;
public class Transparent : MonoBehaviour {
private float duration = .7f;
DateTime time;
bool isStopped=false;
Thread thread;
Color textureColor;
AutoResetEvent resetevent;
// Update is called once per frame void
//----------------------------------------------
public void start_transparency()
{
Debug.Log ("game in start tranparency");
textureColor = this.GetComponent<SpriteRenderer> ().material.color;
thread = new Thread (run);
thread.Start ();
resetevent = new AutoResetEvent(false);
}
//------------------------------------------------
void blink() {
//textureColor.a = Mathf.PingPong(Time.time, duration) / duration;
//this.GetComponent<SpriteRenderer>().material.color = textureColor;
// this could also be a condition indicating "alive or dead"
resetevent.WaitOne();
DateTime now = DateTime.Now;
TimeSpan deltaTime = now - time;
time = now;
textureColor.a = Mathf.PingPong ((float)deltaTime.TotalSeconds, duration) / duration;
//end of if(this.transform.childCount =0)
}
//------------------------------------------------
void run()
{
while (!isStopped) {
time = DateTime.Now;
blink ();
}
}
//------------------------------------------------
private void Update()
{
resetevent.Set ();
this.GetComponent<SpriteRenderer> ().material.color = textureColor;
}
//---------------------------------------------------
private void OnDestroy()
{
thread.Abort();
isStopped = true;
}
//--------------------------------------------
private void OnApplicationQuit()
{
thread.Abort();
isStopped = true;
}
}