- Home /
is there a better way for disabling components?
So I've been finding that for the game I'm working on I need to change/disable components a lot on the next mouse click
for example, I have a script where it generates 10 points as gameobjects, then if you click on one particular point the 3 points closest to it light up. However, obviously you would want those 3 points to change back to their original color if you click on another point (and have the 3 points closest to THAT point light up), repeat etc etc.
I've previously been solving this issue by doing something like this:
void navManager(){
if (checkIfClicked == false){
storePrevWaypoint = gameObject.name;
getWaypoint = GameObject.Find (clickedWaypoint).GetComponent<SpriteRenderer>();
getWaypoint.color = Color.green;
checkIfClicked=true;
Debug.Log ("if");
}
else{
getWaypoint = GameObject.Find (clickedWaypoint).GetComponent<SpriteRenderer>();
getWaypoint.color = Color.green;
getPrevWaypoint = GameObject.Find (storePrevWaypoint).GetComponent<SpriteRenderer>();
getPrevWaypoint.color = Color.black;
storePrevWaypoint =gameObject.name;
Debug.Log("else"); }
it's horrible and clunky in my opinion but I don't know enough c# to make it better. How can I script things like this in a more elegant way?
Answer by HarshadK · May 20, 2015 at 12:13 PM
Since you are storing the name of the gameobject you need to perform a Find and GetComponent each time to get to the SpriteRenderer of the stored gameobject, you can simply store the reference to the SpriteRenderer itself rather than the gameobject name for clickedWaypoint and storePrevWaypoint.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200045im_/https://answers.unity.com/themes/thub/images/avi.jpg)