Procedurally Generated texture showing up on gameobject but .mainTexture returns null?
Hi all,
Not exactly sure whats going on here... I have successfully created an empty Texture2D and filled it in by reading the pixels from a separate rendertexture, then set this new texture to be the .maintexture of my gameobject. In game, the texture is properly displayed on the gameobject, however when I try to set a reference to the texture from a different script (which I need to getpixels from it), the reference returns null. I have debugged it all and the nullreferenceexception is coming from the maintexture. Might this be because I never save the texture to a PNG or something?
Here is the relevant code:
This works in creating and setting the texture: //up camObj.transform.eulerAngles = new Vector3 (-90, 0, 0);
cam.Render ();
Texture2D Up = new Texture2D (resolution, resolution, TextureFormat.RGBA32, false);
//Up.filterMode = FilterMode.Point;
RenderTexture.active = noiseTex;
Up.ReadPixels(new Rect(0, 0, noiseTex.width, noiseTex.height), 0, 0);
Up.Apply();
//applies texture to face
GameObject.Find ("Up").GetComponent ().material.mainTexture = Up;
This is in a separate script, where the gameobject "Up" is Chunk.transform.root.gameObject This always returns null:
//gets texture from side
Texture2D sideTex = Chunk.transform.root.gameObject.GetComponent ().material.mainTexture as Texture2D;
Thanks so much for any help!