- Home /
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 () {
}
}
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.
@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;
}
}
}
}
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;
}
}
}
}
ohhhh I get it now, I'm garbage at coding but the script works now thanks for the help!
Your answer
Follow this Question
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