Check planes that have the same color of gameobject

Hi guys! In my game I have a sphere that rolls on some planes that are created and colored randomly and I want to check when the planes have got the same color of the ball (that is also colored randomly) because I want that when the ball is on a plane that has got its same color, the player must click on the screen to increase the score and to change the color of the sphere. Then, obviously, if the player doesn’t click on the screen when the sphere rolls on the first plane that have its same color the game quit.
How can I do? Now I write the script that I have to create random planes and to change the color of the sphere after touching screen.
Thanks a lot!

SCRIPT TO CREATE RANDOM PLANES.

#pragma strict
 
import System.Collections.Generic;
 
var list : List. = new List.();
 
var s : GameObject; 
var colors = [Color.red, Color.green, Color.blue, Color.yellow, Color.white];
 
function Start () { StartCoroutine(creazione(0.000000000001)); }
 
function creazione(tempo) {
 
     while(true) {
        var pos : Vector3;
        for (var i = 0; i < 30; i++) {
            pos = Vector3(0f,0f,Random.Range(0,100000));
            if (!CheckOverlap(pos)) break;
        }
        if (CheckOverlap(pos)) break;
 
        s = GameObject.CreatePrimitive(PrimitiveType.Plane);
        s.renderer.material.color = colors[Random.Range(0, colors.Length)];
        s.transform.position = pos;
        list.Add(s.transform);
        yield WaitForSeconds(tempo);
    }
}
 
function CheckOverlap(pos : Vector3) {
    for (var t : Transform in list) {
        if (Vector3.Distance(t.position, pos) < 10.0)
            return true;
    }
    return false;
}   

SCRIPT TO CHANGE THE COLOR OF THE SPHERE.

var sphere : GameObject;
		var colors = [Color.red, Color.green, Color.blue, Color.yellow, Color.white];

function Update() {
    if (Input.touchCount == 1) {
        	sphere.renderer.material.color = colors[Random.Range(0, colors.Length)];
    }
}

2 Answers

2

Here is a bit of untested code that Raycasts() down from the center of an object and find and tests the color against the current object:

var hit : RaycastHit;

if (Physics.Raycast(transform.position, Vector3.down, hit)) {
	var rend = hit.collider.renderer;
	if (rend != null) {
		if (rend.material.color == renderer.material.color) {
			// Do something
		}
	}
}

Note I use a direct comparison operator (‘==’) to test the two colors. I’m not sure how Unity handles comparisons of colors. If two colors that appear to be the same you can check with Debug.Log()) are not considered equal, you can check the color distance instead.

function ColorSqrDist(c1 : Color, c2: Color) : float {
	return (c2.r - c1.r) * (c2.r - c1.r) + (c2.g - c1.g) * (c2.g - c1.g) + (c2.b - c1.b) * (c2.b - c1.b));
}

But this script works only with real collisions? I write this script: var hit : RaycastHit; var sphere : GameObject; var colors = [Color.red, Color.green, Color.blue, Color.yellow, Color.white]; if (Physics.Raycast(transform.position, Vector3.down, hit)) { var rend = hit.collider.renderer; if (rend != null) { if (rend.material.color == renderer.material.color) { sphere.renderer.material.color = colors[Random.Range(0, colors.Length)]; } } } and I attached it on the sphere, but nothing happens..

You need to debug it. It will only work if the planes have a mesh collider, and if the first object below your ball is the plane. Put a Debug.Log() statement just inside the 'if' statement to see if it is hitting something, and if so what: Debug.Log(hit.collider.name + ", " + hit.collider.tag); If it is hitting something, then just before doing the comparison, do: Debug.Log(rend.material.color + ", " + renderer.material.color);

I tried, but it seems that nothing happens. How can i be able to check same colors between the two colliders (ball and plane)?

var hit : RaycastHit; if (Physics.Raycast(transform.position, Vector3.down, hit)) { var rend = hit.collider.renderer; if (rend != null) { if (rend.material.color == renderer.material.color) { Debug.Log(hit.collider.name + ", " + hit.collider.tag); } } } This is the script that I attached on my sphere..

As mentioned in my previous comment, it needs to be in Update(). Here is a complete script. #pragma strict function Update() { var hit : RaycastHit; if (Physics.Raycast(transform.position, Vector3.down, hit)) { Debug.Log(hit.collider.name +", " + hit.collider.tag); var rend = hit.collider.renderer; if (rend != null) { if (rend.material.color == renderer.material.color) { Debug.Log("Found a match"); } } } }

I wrote the new answer @robertbu at this URL: Different touches on screen - Unity Answers
Look at it if you have anytime!