- Home /
How do I assign a Texture to a variable?
I've declared a Texture2D variable in JS, and I was wondering how I'm meant to assign it inside the script (not in the inspector) while it's exposed? All of the solutions I've read so only explain the Resources.Load method, but this does not work for an exposed variable.
Thanks in advance.
Answer by Berenger · Feb 15, 2012 at 03:54 AM
I'm not sure what you means by exposed, but as far as I know, Resources.Load works for every variables. It does require the asset to be in the resources folder though, because the load being in the script, Unity can't know if it's used and won't pack it in the build. I don't think it's possible to access assets otherwise. I think there is something with asset bundles, but I'm not sure what that creature is.
An exposed variable is just a variable outside of the function. I can get Resources.Load to work, just not outside of one. Thanks for your time, though.
You should learn some program$$anonymous$$g terms, before starting to programm ;)
There are three kind of variables: local variables, member variables, static variables.
Local variables are only accessible within a certain code block (an if/while block, an function/method, etc.).
$$anonymous$$ember variables are accessible withing the class and are bound to an instance of that class. They are only accessible within the class or if you have an instance for the class (if they have public accessibility)
Static variables are similar to member variables, but they are not bound to a certain instance and can be accessed without having an reference to the instance.
And then, there are also access levels for variables (only for member and static ones), which can be: Private, Protected, Public (depending on language there may be some more, but this are the three most important):
public: can be accessed from outside of the class protected: can only be accessed from within the class and it's derived classes private: can only be accessed by the class they were declared in (they can NOT be accessed by derived classes).
And then if you ask questions, keep in $$anonymous$$d to use this ter$$anonymous$$ology to make sure other people understand what you're trying to ask
I'm not sure if you're just trying to be condescending, but I am fairly familiar with the ter$$anonymous$$ology you've given. It was a variable of type Texture2D, and I'm fairly sure no one was worse off for my use of the phrase.
Your answer
![](https://koobas.hobune.stream/wayback/20220613064108im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do you crop a Texture2d 1 Answer
Repeating Material texture 0 Answers
How To Apply A Texture Atlas For UI Image Objects? 0 Answers
Image is not loading into Texture 0 Answers
What is the best way to implement videos in 2D unity game? 0 Answers