Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Samael_00001 · May 23, 2014 at 03:14 PM · materialfindname

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

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image perchik · May 23, 2014 at 04:46 PM 0
Share

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

avatar image tanoshimi · May 23, 2014 at 05:53 PM 0
Share

Is that because in your scene you have a gameobject called "name", and that object has a material called "mat_name"....?

avatar image Jeff-Kesselman · May 23, 2014 at 05:59 PM 0
Share

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.

avatar image Samael_00001 · May 23, 2014 at 07:27 PM 0
Share

no error messages..

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by LukaKotar · May 23, 2014 at 08:46 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Salty_Stump · Oct 11, 2019 at 03:41 PM

@LukaKotar How would i find it based on color?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges