- Home /
Question by
rbatelaan2 · Jul 21, 2020 at 01:29 AM ·
collisioncolor change
How do I change the color of a gameobject based on number of collisions?
I am trying to change the color of a gameobject based on the number of collisions it is in. With this code, I get the error message: cannot implicitly convert type 'int' to 'bool'
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeColorOnCollision : MonoBehaviour
{
int counter = 0;
void OnCollisionEnter(Collision col)
{
counter ++;
}
void Update()
{
if (counter = 1)
{
transform.GetComponent<Renderer>().material.color = Color.blue;
}
if (counter = 2)
{
transform.GetComponent<Renderer>().material.color = Color.red;
}
}
}
Comment
line 16 and 22 need to be if(counter == number) not if(counter = number)
Best Answer
Answer by lonelycamper · Jul 21, 2020 at 02:10 AM
line 16 and 22 need to be if(counter == number) not if(counter = number)
Your answer
![](https://koobas.hobune.stream/wayback/20220613003630im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to change colors in turn? 1 Answer
Make something change color for a set amount of time 3 Answers
How do I stop characters from standing on top of each other 1 Answer
How to change the color of the ball after hitting square? 5 Answers
change color of a cube having touched by Line Renderer 0 Answers