highlight object color

Hi folks,

i have 5 cylinder objects.. if i click on any one of cylinder the color should be changed and remain unchanged like wise for all those cylinders...

how could i script for that??

i have already tried but no luck....

public int id;

void Update()
{
   if(!clicked)
    renderer.material.color = Color.yellow;

}

void OnMouseDown()
{
   for(id=1; id<=5; i++)
   {
     if(GameObject.Find("cylinder"+i))
     {
       clicked = true;
       renderer.material.color = Color.red;
     }
   }

} 

thanks in advance....sorry about my english...

//You may need to use raycasting to incidate if you have clicked an object in world space.

public class ClyindricalColorz : MonoBehaviour {
    public GameObject[ ] Cylins = new GameObject();
    //depending if you have the Cylins in the scene
    //drag them into the array, 1st making size > 0
    private void UpdateColor(GameObject[ ] myCylins) {
         if (OnMouseDown()) { 
             myCylins[0].renderer.material.color = Color.red;
             //myCylins[1].renderer.material.color = Color.red;
         }

    }

    private void Update() {
        UpdateColor(Clyins[0]);//First Cylindrical GameObject.
        //UpdatColor(Clyins[1]);

    }
}

Take a look at the videos: Burg Zerg Arcade, teaches him to do so (in C #) and also a full RPG.

To your question take a look at:

  1. Unity3D Tutorial - Targeting Enemies 1 / 3
  2. Unity3D Tutorial - Targeting Enemies 2 / 3
  3. Unity3D Tutorial - Targeting Enemies 3 / 3

Links from tutorials:

http://www.burgzergarcade.com/hack-slash-rpg-unity3d-game-engine-tutorial

Problem Solved [highlight object color]

i have tried it with 2 scripts....same 5 cylinders and i noted with id's id=1,2,...5.

its in first "cylinder.cs" script:

public int id;
public static bool cylinderSelected,click,isClicked;
public static int selectedId;
private Color initialColor;

void Start () 
{
  initialColor = renderer.material.color;
}

void Update () 
{
 if(!isClicked)
 {
  renderer.material.color = initialColor;
 }
}

void OnMouseDown()
{
if(id <= 5)
{   
click = true;
selectedId = id;
if(GameObject.FindWithTag("cylinder"+id))
{
isClicked = true;
renderer.material.color = Color.cyan;
}
}
cylinderSelected = true;// i used this for another purpose..dont confuse..
}

now we can control it with "GamePlay.cs" script:

void Update()
{
if(cylinder.click)
{
cylinder.click = false;
cylinder script;
for(int i=1; i<= 5; i++)
{
if(i != cylinder.selectedId)
{
GameObject find = GameObject.FindWithTag("cylinder"+i);
if(find != null)
{
script = find.GetComponent<cylinder>(); // changed the values in cylinder scripts.
script.isClicked = false;
}
}
}
}

thats it....

now it'll come like this, if you click of an object that particular only highlighted..remaining objects wont be highlighted... you may try it...if you have any doubts about it, share it with me....

thanks...