x


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...

more ▼

asked Feb 15 '11 at 09:32 AM

sriram90 gravatar image

sriram90
475 33 37 47

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. ;)

Feb 15 '11 at 10:07 AM StephanK

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...

Feb 15 '11 at 10:38 AM sriram90

hi spree.... have you got my ques???

Feb 15 '11 at 10:46 AM sriram90

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?

Feb 15 '11 at 12:11 PM lampshade

Dont be confused with releasing the mouse button; just put code inside of OnMouseDown() for when you want something to happen when a mouse is clicked.

Feb 15 '11 at 12:13 PM lampshade
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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...

more ▼

answered Feb 17 '11 at 06:30 AM

sriram90 gravatar image

sriram90
475 33 37 47

This code doesn't even come close to compiling. It's hard to even tell what you're doing here.

Jul 23 '12 at 04:21 PM Ben Blaut
(comments are locked)
10|3000 characters needed characters left

//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]);

    }
}
more ▼

answered Feb 15 '11 at 12:19 PM

lampshade gravatar image

lampshade
391 84 92 110

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....

Feb 15 '11 at 12:50 PM sriram90

lampshade's answer only for if you click on a object all objects color should be change....

Feb 17 '11 at 06:36 AM sriram90
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Feb 15 '11 at 01:03 PM

Unamine gravatar image

Unamine
362 14 17 27

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x526
x122
x40

asked: Feb 15 '11 at 09:32 AM

Seen: 2767 times

Last Updated: Jul 23 '12 at 04:21 PM