How to update an object during file based on FileSystemWatcher
I am trying to set a FileSystemWatcher in unity where I need to change the color of an object when a file is updated. There is no error on the code, but the result is not taking action. I am able to change the color of the object if I used the void Start(). but I need to run the code only if a change happened in the desired location.
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO;
public class ObjColorScript : MonoBehaviour { public Color myColor; public MeshRenderer myRenderer;
// Start is called before the first frame update
void Start()
{
string path = @"B:\";
MonitorDirectory(path);
///// New Added
var fileSystemWatcher = new FileSystemWatcher();
fileSystemWatcher.Path = @"B:\";
fileSystemWatcher.Changed += FileSystemWatcher_Changed;
fileSystemWatcher.EnableRaisingEvents = true;
}
///// New Added
void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
myRenderer = GetComponent<MeshRenderer>();
myRenderer.material.color = Color.green;
}
// Update is called once per frame
void Update()
{
//myRenderer = GetComponent<MeshRenderer>();
//myRenderer.material.color = Color.green;
}
} c# unity3d
Comment