- Home /
Texture as return type? From instance to prefab.
Hey
I'm trying to send a texture from one class to another but the best I can achieve is what I think is an empty texture.
I have my class "CreateTaskControl" which is attached to an empty game object. This class instantiates one instance of "DatabaseCom". DatabaseCom has one public Texture "testImage" which I try to send to "CreateTaskControl" (at least as a start).
CreateTaskControl can display the default texture which assign through the editor. I suspect my method of communicating between prefab and instance isn't the best way, but I don't know how else to do it?
//Just the relevant parts of my code:
public class CreateTaskControl : MonoBehaviour {
DatabaseCom _DatabaseCom;
public Texture tempImage;
void Start () {
_DatabaseCom = new DatabaseCom ();
}
void someFunction()
{
tempImage = _DatabaseCom.getTexture(tempInt);
}
void OnGUI()
{
GUI.Box(rectangles[i], tempImage , boxStyle_activeField);
}
public class DatabaseCom : MonoBehaviour {
public Texture testImage;
public Texture getTexture(int ID)
{
return testImage;// [ID];
}
}
you didn't mention that the code gives a warning message when you try to run it - that's important.
it's because you're trying to new
a $$anonymous$$onoBehaviour
. that's not how you do it.
if you wish to use DatabaseCom
as you've defined it then you need to add it as a component. i won't patronize you by posting a link to the docs ;)
did you assign a texture to testImage
in the inspector? that's what someFunction()
will get. if you didn't assign anything, you won't get anything back!
grain of salt/advice: if you're writing c#, try using the na$$anonymous$$g convention. someFunction
is not a good name, SomeFunction
is better, but still not good because it doesn't tell you anything about the function, what it's supposed to do, etc. maybe this is just to demonstrate the problem, but you get the idea...
Your answer
![](https://koobas.hobune.stream/wayback/20220613162838im_/https://answers.unity.com/themes/thub/images/avi.jpg)