Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Referencing an Asset in your Script

Discussion in 'Scripting' started by Kowbell, Apr 13, 2014.

  1. Kowbell

    Kowbell

    Joined:
    Apr 13, 2014
    Posts:
    3
    Howdy.
    I'm fairly fresh to the Unity scene, so pardon this seemlingly novice question. The reason I'm making a post about it instead of googling it is because, well, I don't know how really to ask it. I've tried different combinations, none of which had satisfactory results.

    I'm doing a little script that chooses a texture randomly for my object. The problem I'm having though is how I go about assigning my textures to variables - i.e., do something along the lines of
    Code (csharp):
    1. texturething = Assets/Materials/thetextureiwant.jpg
    2. renderer.material.mainTexture = texturething
    Looking through the Boo documentation on the Unity website, I found AssetDatabase and all of its functions, and figured it would be my knight in shining armor; however, it is only apparently available in the Editor. Ergo, I can not use it in my script.

    So, how do I go about this?

    Thank you!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Use public variables and assign them in the inspector:

    Code (csharp):
    1. public textures as (Texture2D)
    2.  
    3. def Start ():
    4.     renderer.material.mainTexture = textures[Random.Range(0, textures.Length)]
    --Eric
     
  3. Kowbell

    Kowbell

    Joined:
    Apr 13, 2014
    Posts:
    3
    This didn't work, nor does it answer my question of how I access my textures in my script.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    It does work (I had tested it to be sure, since I don't normally write Boo), and it precisely answered your question...look at the code; it shows exactly how to access your textures in your script (along with "assign them in the inspector"). If that's not what you were actually asking, then please rephrase.

    --Eric