- Home /
touch and destroy clone object with specific color
Hello all, i have big problem and i try to solve it 2 days.. try with timers different ways but nothing.. my brain stuck...
here is my code
void FixedUpdate () {
random = boxGenerator.randomColor;
RaycastHit hit = new RaycastHit();
for (int i = 0; i < Input.touchCount; ++i) {
if (Input.GetTouch(i).phase.Equals(TouchPhase.Ended)) {
// Construct a ray from the current touch coordinates
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
if (Physics.Raycast(ray, out hit)) {
if(gameObject.GetComponentInChildren<Renderer>().material.color == colors[random])
{
Destroy (this.gameObject);
scoreScript.increaseScore(score);
Debug.Log("Pathsa " + gameObject.GetComponentInChildren<Renderer>().material.color + " enw " + colors[boxGenerator.randomColor]);
boxGenerator.randomGenerator();
Time.timeScale = 1;
}
else
{
scoreScript.gameOver();
Destroy (this.gameObject);
missionTextScript.gameOver();
gameOverMenuScript.gameOverEnabled = true;
Time.timeScale = 0;
}
}
}
}
}
my game generate a random color and you have to touch a box with the specific color.
when i touch the corect object the game show game over screen ! like i touch wrong object....
i saw that when i press 1 object all with same color destroyed so i think if something is wrong with instatiate and clones...
must to add the prefab and not the clone?
thanks
Some problem you have is you want to check if the object touched is a color box before doing anything. Right now you are not checking anything but the color of the material so basically your function is destroying everything you touch with a specified color.
First of all I second @barbe63's comment.
Secondly you might want to try a something simpler first. Start by just Debug.Logging the color of the objects that you touch. It is possible that there is a discrepancy between the materials color and the random color.
thanks for your answers ! With debugging i found somthing really crazy.. i create new script to handle the touches and send request to destroy the object to tother script atatched to gameobject!
Thanks !!!!
Your answer
![](https://koobas.hobune.stream/wayback/20220613195714im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Difference between Destroy and Destroy immediate (In this case). 2 Answers
Count a touch in half of screen 2 Answers
How to make Touch Input act like GetAxis? 0 Answers
How can I destroy many instantiated objects on endless game 1 Answer
Input.GetTouch never getting TouchPhase.Ended on Android 0 Answers