0

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;
    }



}
Programmer
  • 121,791
  • 22
  • 236
  • 328
ali karimifard
  • 115
  • 2
  • 13
  • 1
    You don't need Thread to fade in Unity. Coroutine is what you need. See the duplicate for more info. – Programmer Sep 25 '17 at 09:28
  • As a remark, always use `volatile` keyword on the flag used as a thread running checks. eg. `volatile bool isStopped` this will ensure the value wouldn't be retrieved from the cache and won't be optimized during the compile time which in most cases ends up with compiling to something like `while ( true )` instead of yours `while ( isStopped )` – mrogal.ski Sep 25 '17 at 09:34
  • @Programmer i need implement this for an unknown time until the moment the user clicks on the object – ali karimifard Sep 25 '17 at 09:36
  • 1.Detect when there is a click on an object. 2.Call the function from the duplicate. What's hard about that? – Programmer Sep 25 '17 at 09:41
  • @Programmer ...no no ....that is fade in and fade out Until the time is unknown.................in coroutine my program Get stuck in a ring ... right? – ali karimifard Sep 25 '17 at 10:51
  • @Programmer ok i find that solution with use coroutine... – ali karimifard Sep 25 '17 at 12:25

0 Answers0