problem with simple script

I have a problem. The purpose of my game is to click on these objects falling at random. You need to click only when the object has the texture red. I had to make two separate scripts. The first script is connected to the main room and is used to create random objects falling down:

Code:

var speed : int = 2;

 

var chrismasBall : GameObject;

 

var instanza;

 

var finds : GameObject;

 

 

 

 

 

 

 

function Start () {

 

 

 

StartCoroutine(Creation(1));

 

 

 

//GameObject.Find("pSphere1").GetComponent("NewBehaviourScript").texBall==[4];

 

 

 

  //cerca funzione "textureRandom" in un'altro script

 

//gameObject.Find("sferacolore").SendMessage("textureRandom");

 

 

 

}

 

 

 

function Update () {

 

Physics.gravity = Vector3(0, -0.05, 0);

 

 

 

 

 

 

 

finds = GameObject.Find("pSphere1").GetComponent("NewBehaviourScript").texBall[5];

 

 

 

 

 

 

 

 

 

 

 

}

 

 

 

 

 

function Creation(tempo){

 

 

 

 

 

   while(true){

 

   

 

   

 

      

 

       instanza = Instantiate (chrismasBall, transform.position, Quaternion.identity);

 

     

 

      

 

      

 

     instanza.AddComponent(Rigidbody);

 

     // instanza.rigidbody.mass = 0.1; 

 

      //instanza.rigidbody.velocity = Vector3(0,1*Time.deltaTime*speed,0);

 

      //instanza.transform.Translate(0,-1,0);

 

      

 

      instanza.transform.position.x = Random.Range(2,-2);

 

      instanza.transform.position.y = 2;

 

      instanza.transform.position.z = 0.2;

 

      instanza.transform.Translate(0, Time.deltaTime, 0);

 

      //instanza.transform.position.y = 12;

 

      //

 

      //Destroy(instanza, 3);

 

      

 

      yield WaitForSeconds(tempo);

 

   

 

   

 

   

 

   }

 

 

 

}

 

 

 

 

 

/*function OnMouseDown(){

 

 

 

if(renderer.material.mainTexture == finds[5]){

 

 

 

print("cliccato!");

 

 

 

}

 

 

 

 

 

 

 

}*/

The second script is attached to objects and is used to randomly change the texture of the object. For textures I used array but I have to call it in the script attached to the camera so that I recognize what object I clicked. Here is the second script. How can I do? thanks

Code:

var texBall : Texture[] = new Texture [5];

 

 

 

function Start(){

 

 

 

textureRandom();

 

 

 

 

 

}

 

 

 

 

 

 

 

function textureRandom(){

 

 

 

 

 

 

 

while(true){

 

    

 

    yield WaitForSeconds(1); //aspetto 1 secondo e cambio colore alla mesh

 

    

 

    var randomtex; // variabile randomColor

 

    

 

    randomtex = texBall[Random.Range(0, texBall.length)]; //random della variabile cambiaColore

 

    

 

      

 

   renderer.material.mainTexture= randomtex; // lo assegno alla variabile randomColor

Did you proof-read this at all?

My god, that code is horrible to read. Please, clean that up so we can read it.

this is scipt for Main camera

var speed : int = 2;
var chrismasBall : GameObject;
var instanza;
var trova2;


function Start () {

StartCoroutine(Creation(1));



}

function Update () {
Physics.gravity = Vector3(0, -0.05, 0);

}


function Creation(tempo){

          while(true){
   
   
      
       instanza = Instantiate (chrismasBall, transform.position, Quaternion.identity);
     
      
      
     instanza.AddComponent(Rigidbody);
     
      
      instanza.transform.position.x = Random.Range(2,-2);
      instanza.transform.position.y = 2;
      instanza.transform.position.z = 0.2;
      instanza.transform.Translate(0, Time.deltaTime, 0);
      
      yield WaitForSeconds(tempo);
   
   
   
   }

}

This is code for texture random

var texBall : Texture[] = new Texture [5];
 var trovato : GameObject;

function Start(){

textureRandom();

}

function update(){
}



function textureRandom(){



while(true){
    
    yield WaitForSeconds(1); //aspetto 1 secondo e cambio colore alla mesh
    
    var randomtex; // variabile randomColor
    
    randomtex = texBall[Random.Range(0, texBall.length)]; //random della variabile cambiaColore
    
      
   renderer.material.mainTexture= randomtex; // lo assegno alla variabile randomColor
    

   }
}

function OnMouseDown(){
print("Cliccato su palla");

}