So, these are some really basic scripting questions, but until now I’ve found quick simple workarounds. I now want to know how to do these things.
1 - How can I reference an object (say a material) in the project directory for use in a script? Say I want to set an objects material through script, and I don’t want to declare a variable so that I can do so in the GUI.
2 - How do I go about interacting between two scripts? How can I see what the value of certain variable in script 1 is, from script 2?
Thanks guys!
- there are 2 main ways. If you put, for a texture (javascript):
var myTexture : Texture2D;
There will be a “slot” on the script component, and you can drag the desired texture from the project pane up to there.
Alternately, if you put the texture in Resources/ in the project folder, you can get a reference to it with:
function Start() {
myTexture = Resources.Load("Texture filename");
}
This isn’t the preferred method though; since the engine doesn’t know which objects in Resources/ will need to be accessed, the entire Resources folder is included in every build (even if you only use one texture from it, but have gigabytes stored in there). When using drag-and-drop it knows to only include the resources that are specifically referenced when it builds.
2 -
function Update() {
tempVar = GetComponent(OtherScriptName).otherScriptVariableName;
print(tempVar);
}
your first solution to my first problem is what I have been doing, however its quite a pain when this script is used on several objects. Linking the material in Unity’s GUI can get tiresome. Its also a waste of time whenever that material or object is changed. Was hoping there was a better way 
Thanks!
Not sure I quite follow…if you change the material, then the changed material would be automatically used wherever you reference it. Anyway, you could combine the techniques and reference the material once on one script using the GUI, and then use GetComponent (or a static variable) to reference that one variable from other scripts.
–Eric