- Home /
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...
So you want the color for all 5 cylinders changed to red when you click on any one of them? And it should change back to yellow when you release the button? Just trying to understand the question. ;)
nope spree...clicked cylinder only change the color to red..all others remains in yellow...if you click any one of them that particular cylinder color only to be change... hope you understand...
So I am a green. I click a cyl and they ALL Change to green? If I am a red, they all change from green to red?
Dont be confused with releasing the mouse button; just put code inside of On$$anonymous$$ouseDown() for when you want something to happen when a mouse is clicked.
Answer by sriram90 · Feb 17, 2011 at 06:30 AM
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...
This code doesn't even come close to compiling. It's hard to even tell what you're doing here.
Answer by lampshade · Feb 15, 2011 at 12:19 PM
//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]);
}
}
hi lampshade.... ya i tried your script...i dont want all those objects changed the color...i just want if i click on a cylinder that particular only change as red....consider if i click on cylinder1 that only should be red..others are yellow.....if i click on cylinder2 all other changed as yellow and cylinder2 only as red...like this....have u got my ques now??? hope you understand well....
lampshade's answer only for if you click on a object all objects color should be change....
Answer by Unamine · Feb 15, 2011 at 01:03 PM
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:
- Unity3D Tutorial - Targeting Enemies 1 / 3
- Unity3D Tutorial - Targeting Enemies 2 / 3
- Unity3D Tutorial - Targeting Enemies 3 / 3
Links from tutorials:
http://www.burgzergarcade.com/hack-slash-rpg-unity3d-game-engine-tutorial