- Home /
Checking for null against Texture2D
Hi, I've just faced the error saying: NullReferenceException, and I'm considering it has something to do with Texture2D, rather than C# language itself. The following is the code I'm trying to run:
using UnityEngine; using System; using System.Runtime.InteropServices; public class VncScreen : MonoBehaviour { [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate void UpdateTexDele(IntPtr ptr, int x, int y, int w, int h); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate void CreateTexDele(int w, int h); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate void UnityLogDele(string str); [DllImport("libvncclient")] private static extern int unity_start(int argc, string[] argv, UpdateTexDele dele1, CreateTexDele dele2, UnityLogDele dele3); [DllImport("libvncclient")] private static extern void unity_update(); private Texture2D tex=null; void Start() { unity_start(2, new string[] { "Qemu", "172.30.1.32" }, GetTexPtr, CreateTex, UnityLog); } void Update() { unity_update(); } void CreateTex(int width, int height) { Debug.Log(width + ", " + height); if (tex == null) { tex = new Texture2D(width, height, TextureFormat.ARGB32, false); //GetComponent<MeshRenderer>().material.mainTexture = tex; } } void GetTexPtr(IntPtr ptr, int x,int y, int w, int h) { if (tex != null) { /*Color[] colors = new Color[w * h]; unsafe { byte* buf = (byte*)ptr; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { colors[w * i + j] = new Color(buf[0] / 255.0f, buf[1] / 255.0f, buf[2] / 255.0f, buf[3] / 255.0f); buf += 4; } } }*/ //tex.SetPixels(x, y, w, h, colors); //tex.Apply(false); } } void UnityLog(string str) { Debug.Log(str); } }
The code is a bit complicated because it highly depends on the library written in C. The runtime error occurs when it performs comparison between the variable tex and null. As you can see, tex is just the member variable, and the statement doesn't seem to contain any illegal operations. What could be the problem? link text
Edit: I think I screwed up when typing code, sorry about that. I'm posting the code with an extension modified to *.txt
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Can't load Mouse Cursor texture 1 Answer
Distribute terrain in zones 3 Answers