- Home /
Fatal error after loading image and drawing texture
Hey guys, I'm trying to implement a simple script to display images. The images load fine and are drawn to the screen but after about 15 seconds a fatal error occurs and Unity crashes. Here's my code stripped down...
void Start () {
xu = Screen.width;
yu = Screen.height;
www = new WWW(@"file://D:\Slides\Assets\Final Renders\IM_00.jpg");
}
void OnGUI() {
GUI.DrawTexture(new Rect(0,0,xu/2,yu), www.texture, ScaleMode.ScaleToFit);
}
Here is the error I'm getting...
When the 'GUI.DrawTexture(new Rect(0,0,xu/2,yu), www.texture, ScaleMode.ScaleToFit);' line is commented out no error occurs. The image is a JPEG and roughly 140KB. If anyone can point me in the right direction as to what I'm doing wrong it will be greatly appreciated! Cheers
Answer by Rob-Fireproof · Apr 28, 2014 at 05:09 PM
I think it's probably that you're trying to render the texture before it's actually finished downloading (the WWW class will start the download immediately, but it still takes time).
Try changing your OnGUI to something like...
void OnGUI()
{
if (www.isDone)
{
GUI.DrawTexture(new Rect(0,0,xu/2,yu), www.texture, ScaleMode.ScaleToFit);
}
}
Thanks for the quick reply Rob, I thought this should have done the job after reading your comment but unfortunately the same error was thrown up... for anyone who has the same problem I just worked around it by creating a plane and adding 'renderer.material.mainTexture = www.texture;' rather than the GUI stuff that causes the problem. Cheers!
Your answer
Follow this Question
Related Questions
gc: too many threads, crash using Unity 4.2 8 Answers
The best way to load scenes ingame 3 Answers
Unity Game Crashes on load using Nox 0 Answers