How to read assets folder via route partially generated with a for-loop?
Hi buddies:
This is the project structure (same for all second level folders):
This is the code:
public class Checker : MonoBehaviour
{
public Terrain terrain;
List<Material> grounds = new List<Material>();
public float maxTime = 5.0f;
public float currentTime;
// Start is called before the first frame update
void Start()
{
terrain = this.gameObject.GetComponent<Terrain>();
for (int i = 0; i < 10; i++)
{
grounds.Add(Resources.Load<Material>("Materials/Ground" + i + "/Suelo" + i));
}
}
// Update is called once per frame
void Update()
{
if (currentTime >= maxTime)
{
currentTime = 0;
for (int i = 0; i < grounds.Count; i++)
{
terrain.materialTemplate = grounds[i];
}
}
else
{
currentTime += Time.deltaTime;
}
}
}
So far this is the result: https://gyazo.com/5eece4bdd776cb6f1e5ca9d52797c122
I'm sure I'm not filling the array properly by either not getting anything as the route may be incorrect, or maybe the "Ground" + i for the route doesn't work. Either way, I'm at an impass, and would very much appreciate a little push from you buds.
Thanks a lot in advance.
Comment
Answer by xxmariofer · Sep 24, 2021 at 10:53 AM
If you want to load a material using
Resources.Load<Material>
that material must be in a folder called Resources. The directory tree would be
Assets/Resources/Materials/Suelo0.mat
Check how Resources folder work here:
https://docs.unity3d.com/ScriptReference/Resources.Load.html