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 Sphyx · Oct 03, 2013 at 08:25 PM · gameobjectmaterialarraysoncollisionenterswitch

GameObject Array, Resources Load, OnCollisionEnter and Material

Here is the code folks:

 using UnityEngine;
 using System.Collections;
 
 public class LeBrick : MonoBehaviour {
     GameObject[] objMatToCompTo;
     // Use this for initialization
     void Start () {
         objMatToCompTo = Resources.LoadAll("(1)BlockPreFab") as GameObject[];
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     void OnCollisionEnter(Collision col)
     {
         if (col.gameObject.name == "Ball")
         {
             Debug.Log("hit");
             switch(gameObject.renderer.material.name)
             {
                 case "1":
                     Debug.Log("destroyed?");
                     Destroy(gameObject);
                     break;
                 case "2":
                     gameObject.renderer.material = objMatToCompTo[0].renderer.material;
                     break;
                 case "3":
                     gameObject.renderer.material = objMatToCompTo[1].renderer.material;
                     break;
                                 default:
                                         break;
                   }
           }
     }
 }

hit! prints, destroyed doesnt...

I worked this a few hours, and still can't find the problem...

appreciate any help! =]

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by YoungDeveloper · Oct 03, 2013 at 08:26 PM

Didn't know you can use strings in switch case, or can't you?

Comment
Add comment · Show 7 · 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 Sphyx · Oct 03, 2013 at 08:30 PM 0
Share

http://msdn.microsoft.com/en-us//library/vstudio/06tc147t.aspx

"In the final example, string input is converted to an integer variable, switchExp, which is used for the switch expression. You also can use the string variable, str, directly. To do that, you would change the case labels to specify string values, as is shown in the following code." <- $$anonymous$$icrosoft quote....

avatar image Sphyx · Oct 03, 2013 at 08:34 PM 0
Share

I dont think you are correct...

avatar image tw1st3d · Oct 03, 2013 at 08:35 PM 0
Share

You can use strings in switches. Why wouldn't you be able to?

 switch(guys_name)
 {
     case "John":
         break;
     case "Doug":
         break;
 }
avatar image Sphyx · Oct 03, 2013 at 08:37 PM 0
Share

This is what im trying to do, with the code above...

Correct question: Why im not able to?

avatar image YoungDeveloper · Oct 03, 2013 at 09:27 PM 0
Share

Add a debug message, by this you will know are you even entering the switch.

 switch(gameObject.renderer.material.name)
 {
 Debug.Log("going in switch works");
 ]
Show more comments
avatar image
0

Answer by ArkaneX · Oct 04, 2013 at 05:00 PM

The problem might be related to use rendered.material.name in your switch statement. When you do it, a new material is created (an instance of the material you initially set in your object). So, if you set material "1" in inspector, renderer.material.name will return "1 (Instance)". You can test it by inserting

 print(renderer.material.name);

inside switch, after default.

To correct this, you can change your switch statement to:

 switch(gameObject.renderer.sharedMaterial.name)

You can also test for instance names as well:

 case "1":
 case "1 (Instance)":
     Debug.Log("destroyed?");
     Destroy(gameObject);
     break;

though I would probably go with the first solution. But before actually changing your code, I strongly suggest to read and understand the difference between Renderer.material and Renderer.sharedMaterial.

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

18 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

Related Questions

Camera switch between child back to main camera issue 2 Answers

OnCollisionEnter Collision not detected? 1 Answer

OnCollisionEnter GameObject throws NullReferenceException 0 Answers

how to check if an game object had been collided with another game object form its left or right side? 1 Answer

How to create a complex material in an Editor-Script? C# 0 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