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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Resources.LoadAll docs

Discussion in 'Editor & General Support' started by jister, Mar 13, 2016.

  1. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    hey the docs for Resources.LoadAll(); gives the following example:

    Code (CSharp):
    1. // Loads all assets in the "Resources/Textures" folder
    2. // Then picks a random one from the list.
    3. // Note: Random.Range in this case returns [low,high)
    4. // range, i.e. the high value is not included in the range.
    5.  
    6. using UnityEngine;
    7. using System.Collections;
    8.  
    9. public class ExampleClass : MonoBehaviour {
    10.     void Start() {
    11.         GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
    12.         Texture2D[] textures = (Texture2D[]) Resources.LoadAll("Textures");
    13.         Texture2D texture = textures[Random.Range(0, textures.Length)];
    14.         go.GetComponent<Renderer>().material.mainTexture = texture;
    15.     }
    16. }
    to my experience this doesn't not work, is that correct?
    the following does however work:
    Code (CSharp):
    1. // Loads all assets in the "Resources/Textures" folder
    2. // Then picks a random one from the list.
    3. // Note: Random.Range in this case returns [low,high)
    4. // range, i.e. the high value is not included in the range.
    5.  
    6. using UnityEngine;
    7. using System.Collections;
    8.  
    9. public class ExampleClass : MonoBehaviour {
    10.     void Start() {
    11.         GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
    12.         Texture2D[] textures = Resources.LoadAll<Texture2D>("Textures")as Texture2D[];
    13.         Texture2D texture = textures[Random.Range(0, textures.Length)];
    14.         go.GetComponent<Renderer>().material.mainTexture = texture;
    15.     }
    16. }
    is this the general experience with LoadAll? if so maybe the docs could be updated to this.
    it didn't take that long to find out but still it's wasted precious development time.
     
  2. Ignacio-Liverotti

    Ignacio-Liverotti

    Unity Technologies

    Joined:
    Jul 23, 2015
    Posts:
    36
    Hello jister,

    Thank you for reporting this issue. I have passed the comment along to the Documentation Team so that they can look into it.

    Best,
     
    jister and Laurakh like this.