- Home /
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
I assume you have something other than "do smthng..." in your actual code. Assu$$anonymous$$g you do, we need to know what that is, because what you've posted should work
Is that because in your scene you have a gameobject called "name", and that object has a material called "mat_name"....?
Debugging is all about checking your assumptions.
Break out each assumption and check to make sure it is true. Eg
GameObject obj = GameObject.Find("name");
Debug.Log("Game object is "+obj);
string matName = obj.renderer.material.name;
Debug.Log("$$anonymous$$aterial name is "+matName);
Also, is this generating any error messages on run? I suspect you are getting a null pointer exception. Always include the complete error message when asking for help.
Answer by Samael_00001 · May 23, 2014 at 08:19 PM
It turns out that material name is something like "mat_name (Instance)" o_O
Answer by LukaKotar · May 23, 2014 at 08:46 PM
There are three possible reasons that come to mind:
The script is not attached to an object in the scene, or the object/script is disabled.
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)The condition in the
if
statement is always true, and theelse
statement is never executed. (Theelse
statement will only run ifGameObject.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.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Help with a spawner script 1 Answer
Plane - 10x10 grid - Questions about it. 1 Answer
importing a blender model amd making it look the same 0 Answers
Find texture by name? Or other types? 2 Answers