Hello, I know how to do this, but how do I make it only change the one font color that I want to change?
Save the textMesh you want to change and use the following.
You’re not even trying: Search it next time
Hey, I saw that… I said I knew how to change the color. But it changes all of them in every scene to that color.
Read all of my input. Save the textMesh you want to change inside your code, then use that saved variable to change it. If you assign the same function to everything and you say .font.material = Color.red or something similar they are all going to run the same thing. Your not giving us any code to go by so this is harder to solve then it would otherwise be. Complete explanation would be nice (help us help you please)
Here ya go.
var t : TextMesh;
function Start(){
t = transform.GetComponent(TextMesh);
}
function OnMouseDown () {
PlayerPrefs.SetString("Car Name", "Car5");
t.font.material.color = Color.yellow;
}
Are you using the above code (is it attached) to each object your having problems with?
I’m guessing that’s the case in which case that is your problem, you need a way to differentiate the ones you want to change vs the ones you don’t. How does your code know which to change? Because it looks to me when your mouse is down it changes all textMesh of every object that has that script attached and changes it to yellow.
Yes. It is attached to the one I want to change the color of. How would I change it? I don’t think you can change the name of a component.
You don’t want to change the component name, you want to find a way of figuring out what your mousing over and change only that. I personally assign everything a unique name and then use a raycast to find the name of what my mouse is on. From there I compare what I’m selecting with who’s name I’m currently on or a list of things and then only change it when the names of the raycast and the name I’m looking at are the same.
I personally use the following for my own game (note: it’s in OnGUI() to make use of the mouse position).
if(Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray.origin, ray.direction, out hit, 1000))
{
if(hit.transform.gameObject.name == WhicheverObjectYouWantToChange)
{
}
}
}
Thanks. I’ll test it and tell you the results here.
I get a lot of errors.
function OnMouseDown () {
PlayerPrefs.SetString("Car Name", "Car5");
}
function OnGUI(){
if(Input.GetMouseButtonDown(0)){
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray.origin, ray.direction, out hit, 1000)){
if(hit.transform.gameObject.name == "ProbeSelectGreen"){
guiText.material.color = Color.green;
}
}
}
}
EDIT: is your code in C#? I think it is. lemme try converting it.
yeah the code is in C# and the
Ray ray = ...;
is supposed to be
ray : Ray = ...;
but not just that, all variable declarations have to be edited.
Fixed.