- Home /
My foreach or the Resources.LoadAll Is not getting the information I need
This is an update on my latest problems. I found out that either my Resources.LoadAll is not getting the material info from my folder or that the foreach loop is not working right.
{ private GameObject Player;
private Material[] ColorMaterial;
private Renderer rend;
public int ColorID;
private void Start()
{
Player = GameObject.Find("Player");
rend = Player.GetComponent<Renderer>();
ColorID = 0;
ColorMaterial = Resources.LoadAll<Material>("Assets/Resources/Colors/");
}
private void Update()
{
Player.transform.Rotate(0, 10 * Time.deltaTime, 0);
}
public void ColorChanger()
{
if (ColorID < 4)
{
ColorID++;
}
else if (ColorID == 4)
{
ColorID = 0;
}
foreach (Material CurrentColor in ColorMaterial)
{
if (CurrentColor.name.Equals("Color_" + ColorID))
{
rend.sharedMaterial = CurrentColor;
}
}
}
public void BackToLoad()
{
SceneManager.LoadScene(1);
}
}
Simple debugging (logging out every material's name) should easily clarify what the foreach is able to find... no need to guess if it works as you expect or not. You could also log out the value of CurrentColor.name.Equals("Color_" + ColorID)
and ColorID
to make sure they match your expectations and don't fail because of for example a misplaced whitespace.
99% of program$$anonymous$$g bugs happen because your idea of what's happening in the code is different from what is actually happening in the code. The only way to fix bugs is to find out what ACTUALLY happens in the code and where your assumptions are going wrong. So just print out all that's in the array and the result of every if-condition you test for. This way you should find out if things are wrong already when entering the loop or if something is wrong with the names / if-check.
int i = 0;
Debug.Log("Color$$anonymous$$aterial has " +Color$$anonymous$$aterial.Length+ " items");
foreach ($$anonymous$$aterial CurrentColor in Color$$anonymous$$aterial)
{
Debug.Log("$$anonymous$$aterial " + i + " in Color$$anonymous$$aterial array is " + CurrentColor);
Debug.Log("ColorID is " + ColorID + ". Checking for name 'Color_" + ColorID + "'"));
Debug.Log("Do they equal? " + CurrentColor.name.Equals("Color_" + ColorID));
i++;
if (CurrentColor.name.Equals("Color_" + ColorID))
{
rend.shared$$anonymous$$aterial = CurrentColor;
}
}
Sry. $$anonymous$$ight contain typos... I'm on mobile
Answer by tanoshimi · Apr 21, 2017 at 07:06 AM
"Not getting the information you need" is not very descriptive - what materials were you expecting? And which did you get instead? A screenshot of the hierarchy of your project showing where these materials are located would help too.
However, if I were to guess, I'd say that you've supplied the wrong string here:
ColorMaterial = Resources.LoadAll<Material>("Assets/Resources/Colors/");
You don't need the "Assets" bit - all assets are contained in Assets. And I don't think you want the trailing slash either, but not near a computer to check currently. Try this:
ColorMaterial = Resources.LoadAll<Material>("Resources/Colors");
Actually, I figured it out on my own and it was the string in the Resources.loadall, like you said from before. So, thank you for your help.
Answer by HarshadK · Apr 20, 2017 at 05:24 AM
I do not see anything in the above code that handles the key press event. You need to add this to your Update()
method.
private void Update()
{
Player.transform.Rotate(0, 10 * Time.deltaTime, 0);
PlayerPrefs.Save();
if(Input.GetKeyDown(KeyCode.Space)) {
ColorChanger();
}
}
you still need to use
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) {
}
though
I don't get what you mean. I have it hooked up to a UI button in the scene that runs the ChangeColor function. I don't think doing an input will be any different.
Check if LoadAll is in fact loading your materials. Just put following code in your Start()
foreach(var mat in Color$$anonymous$$aterial) {
Debug.Log(mat.name);
}
after
Color$$anonymous$$aterial = Resources.LoadAll<$$anonymous$$aterial>("Assets/Resources/Colors/");
If it prints out your material names then they are loaded, otherwise your issue lies there.
If issue is not in loading then check if "Color_" + ColorID
in fact produces a name that matches your material name.
Actually, it was this problem. I noticed it when I turn the material array to a public and notice it wasn't getting any information. It was the string in Resoucres.loadall function that I messed. So, thank you again for your help.
Your answer
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
Changing two different objects renderer colour 1 Answer
Change Color of Material in Loop 1 Answer
What is the best way to have different colored material on a batch of the same 3D object? 1 Answer
i'm trying to code a premise, if 3 objects are the same color then it will destroy the wall 1 Answer