- Home /
want to switch between material when triggered, but facing weird problem. help.
I used this following script
public void OnCollisionEnter(Collision other)
{
if(other.gameObject.tag == "Player")
{
if (this.gameObject.GetComponent<Renderer>().material = onn)
{
this.gameObject.GetComponent<Renderer>().material = offf;
Debug.Log("turned off");
}
if (this.gameObject.GetComponent<Renderer>().material = offf)
{
this.gameObject.GetComponent<Renderer>().material = onn;
Debug.Log("turned on");
}
}
}
when player collides once, this function should be carried out once. but it is carried out several times. I'm getting both "turned off" and "turned on" messages.
what are some other methods to execute this problem ??
thanks in advanced
Answer by Hellium · Jan 30, 2019 at 09:13 AM
private int collisionsCount = 0 ;
private Renderer renderer;
private void Start()
{
// Save a reference to the renderer component for better performance
renderer = GetComponent<Renderer>().material;
}
public void OnCollisionEnter(Collision other)
{
// Use CompareTag for better performances
if(other.gameObject.CompareTag( "Player") )
{
collisionsCount++ ;
if( collisionsCount % 2 == 0 ) // Check if the number of collision is odd or even
{
renderer.material = onn ;
Debug.Log("turned on");
}
else
{
renderer.material = offf ;
Debug.Log("turned off");
}
// Or, for a shorter solution
// renderer.material = ( (++collisionsCount) % 2 == 0 ) ? onn : off;
}
}
Answer by Turlok · Jan 28, 2019 at 06:35 PM
I'm not in a position to test right now, but I can see two issues with your code. First, the if conditions are actually assignment operations and not boolean conditionals so try changing:
if (this.gameObject.GetComponent<Renderer>().material = onn)
to
if (this.gameObject.GetComponent<Renderer>().material == onn)
Second, after switching onn to offf you're immediately checking if it's offf and changing it back to onn, so change:
if (this.gameObject.GetComponent<Renderer>().material = offf)
to
else if (this.gameObject.GetComponent<Renderer>().material == offf)
sorry that doesnt work, I stopped getting either of the message (turned off, turned on)
in that case, are onn and offf both of type $$anonymous$$aterial? Also, does the object the code is checking already have either onn or offf as it's material? It might be safer to make an enum variable representing which materials you want to apply, and then check the object's current enum to apply the desired material.
yes, gameobject already have onn or offf material on it.
Answer by king_opping · Jan 30, 2019 at 09:13 AM
hi, what can i see here is a need more basic programming definition. because when you declare if statement it means you compare the logic, it is different between "=" and "=="
"=" is used to assign value to a variable "==" is used to compare logic
so your code i think should be like this...
public void OnCollisionEnter(Collision other)
{
if(other.gameObject.tag == "Player")
{
if (this.gameObject.GetComponent<Renderer>().material == onn)
{
this.gameObject.GetComponent<Renderer>().material = offf;
Debug.Log("turned off");
}
if (this.gameObject.GetComponent<Renderer>().material == offf)
{
this.gameObject.GetComponent<Renderer>().material = onn;
Debug.Log("turned on");
}
}
}
hope it helps.
Answer by surfuay · Jan 31, 2019 at 12:22 AM
hi, if you haven't solved it yet I think the problem is the opening line
you're using OnCollissionEnter
I think what you're looking for is
OnTriggerEnter
then your following script of course you'll need to have the box "IsTrigger" checked off in each game object that is using any reference to this script. also this might be irrellevant but when i've had the parenthesis for the Collision i write mine as Collider
so
public void OnTriggerEnter(Collider other) { the rest of your code }
idk why tou think OnTriggerEnter is better for that case, and what would be changed to replace the OnCollisionEnter()
to the current purpose I think he archieve exactly the same result, please illustrate us why change that.
but if I checked isTrigger, player will pass right through the game object, I dont want that. I want player to collide and bounce back.