if (GameObject.Find("name").renderer.material.name == "mat_name") {do smth..}

if (GameObject.Find(“name”).renderer.material.name == “mat_name”)
{
Application.LoadLevel(“Game_Over”);
}
else
{
do smthng…
}

Event “do somthng” never happens

There are three possible reasons that come to mind:

  1. The script is not attached to an object in the scene, or the object/script is disabled.
  2. The function is never called (If the statement is in the Update() function, you don’t need to worry about that. Otherwise, you will need to call the function manually. For example: if the function is called “GameOver()”, you can call it by simply putting “GameOver();” somewhere in the code)
  3. The condition in the if statement is always true, and the else statement is never executed. (The else statement will only run if GameObject.Find("name").renderer.material.name is not "mat_name", or if the object “name” is not found)

You may wish to debug the code:

//If you are using JavaScript, the 'void' keyword would be 'function' instead
//I only use "GameOver()" as an example
void GameOver ()
{
    Debug.Log("GameOver function was called");
    if (GameObject.Find("name").renderer.material.name == "mat_name")
    {
        Debug.Log("Game over!");
        Application.LoadLevel("Game_Over");
    }
    else
    {
        Debug.Log("Not over yet...");
    }
}
  • If you see “GameOver function was called”, you will know that this is not the issue.
  • If you see “Game over!”, it means that the object was found, and the material name is “mat_name”.
  • If you see “Not over yet…”, it means that the else statement was reached.

Based on this information, you should be able to identify the problem.

Note that you can click the “Collapse” button in the console to avoid spam.

It turns out that material name is something like “mat_name (Instance)” o_O

@LukaKotar How would i find it based on color?