Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Whalesmack · Jan 14, 2018 at 07:31 AM · collisionmaterialcollision detectionbool

Why is the material switching back after collision?

I'm making a tile puzzle where you need to change all the tiles to a certain color and the way you change the color is to move the player over the tile. I have it set so that on each input the player is automatically moved into the center of a tile where I put a trigger. My goal is to have the tile change material each time the player enters a trigger, from corrupt material to pure material and back again each time the player triggers this script. For some reason though whenever the player enters the trigger the material changes to Pure for about 1 frame and then switches back to CorruptTileMat. The collision is only happening once so I'm not sure why its changing back each time rather than staying the new color. also no matter what material the tile is its automatically changed to CorruptTileMat, not sure why that is happening either. I'm quite noob at coding so any help is appreciated! Here is the code I wrote.

using UnityEngine; using System.Collections;

public class CorruptTile : MonoBehaviour {

 public Material CorruptTileMat;
 public Material Pure;
 public GameObject CTile;
 private bool Corrupted;


 private void FixedUpdate()
 {
     if (CTile.GetComponent<Renderer>().material = CorruptTileMat)
     {
         Corrupted = true;
     }
 }

 private void OnCollisionEnter(Collision collisionInfo)
 {
     if (collisionInfo.collider.name == "Player")
     {

         if (Corrupted == true)
         {
             
             Debug.Log("Collision Detected");
             CTile.GetComponent<Renderer>().material = Pure;

         }

         else if (Corrupted == false)
         {
             Debug.Log("Collision Detected");
             CTile.GetComponent<Renderer>().material = CorruptTileMat;
         }

         

     }

     
 }

 void Update () {


     
 }

}

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

1 Reply

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

Answer by Bonfire-Boy · Jan 14, 2018 at 04:37 PM

 if (CTile.GetComponent<Renderer>().material = CorruptTileMat)

A single = is assignment, so your FixedUpdate is setting the material, not testing it.

But it's not clear to me why you're doing this check in FixedUpdate anyway; why not set the flag in OnCollisionEnter, when it becomes corrupted? And in any case, FixedUpdate doesn't look like the right place for it, since it's not physics-related.

Comment
Add comment · Show 3 · 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 Whalesmack · Jan 14, 2018 at 10:18 PM 0
Share

@Bonfire-Boy Huh, I didn't even think about where I was putting the check. I changed the code to this which fixed the rapid switch problem and is way neater but the material won't change back to corrupt after it has turned pure. Is there something wrong with my else if? public class CorruptTile : $$anonymous$$onoBehaviour {

 public $$anonymous$$aterial CorruptTile$$anonymous$$at;
 public $$anonymous$$aterial Pure;
 public GameObject CTile;
 private bool Corrupted;


 private void OnCollisionEnter(Collision collisionInfo)
 {
     if (collisionInfo.collider.name == "Player")
     {

         if (CTile.GetComponent<Renderer>().material = CorruptTile$$anonymous$$at)
         {
             Corrupted = true;
         }

         if (Corrupted == true)
         {                
             Debug.Log("Collision Detected");
             CTile.GetComponent<Renderer>().material = Pure;
         }

         else if (Corrupted == false)
         {
             Debug.Log("Collision Detected");
             CTile.GetComponent<Renderer>().material = CorruptTile$$anonymous$$at;
         }

     }
    
 }
 

}

avatar image Bonfire-Boy Whalesmack · Jan 14, 2018 at 10:46 PM 1
Share

Sorry, I obviously wasn't clear enough. This is how to test if the material is the corrupt one...

if (CTile.GetComponent<Renderer>().material == CorruptTile$$anonymous$$at)

Note that I've changed your = to ==. Your line is setting the material, not testing it.

As for the other thing, I meant more like this...

 private void OnCollisionEnter(Collision collisionInfo)
  {
      if (collisionInfo.collider.name == "Player")
      {
 
          if (Corrupted == true)
          {                
              Debug.Log("Collision Detected");
              CTile.GetComponent<Renderer>().material = Pure;
              Corrupted = false;
          }
          else if (Corrupted == false) // this should just be an else, the if part is redundant
          {
              Debug.Log("Collision Detected");
              CTile.GetComponent<Renderer>().material = CorruptTile$$anonymous$$at;
              Corrupted = true;
          }
 
      }
     
  }
  
 }
avatar image Whalesmack Bonfire-Boy · Jan 14, 2018 at 11:30 PM 0
Share

ohhhh I get it now, I'm garbage at coding but the script works now thanks for the help!

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

130 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Can I have a single rigidbody2d that acts as both collider and trigger? 1 Answer

Objects don't collide 1 Answer

Is there a better way to check for a collision with a prefab than by name or tag? 2 Answers

Collision & 2D 2 Answers

how to make an Explosive rigidbody trigger by another collision box 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