system
1
Component comps = this.gameObject.GetComponent();
for(int i=0;i<comps.length;i++)
{
Component c = comps*;*
Type t = c.GetType();
if(t.FullName == “TrackRenderer”)
{
- this.gameObject.GetComponent().enabled = true;*
}
else
{
this.gameObject.GetComponent().enabled = false;
}
}
The error I get with this is:
The type or namespace name ‘c’ could not be found.
I have a few gameobjects with this script attached to it, each one has a few different scripts attached to it, but they always have a TrackRenderer.
The thing I want to do is disable all those scripts and keep the TrackRenderer enabled.
Anyone have suggestions?
The reason you get the error is that c is a variable, and not a type. Here is one way to do it: set all components inactive, and then simply switch on the TrackRenderer.
Note, you don’t have to reference this.gameObject, you can use the method inherited from MonoBehaviour straight.
Edit: Incorporated the comments below.
MonoBehaviour[] comps = GetComponents<MonoBehaviour>();
foreach(MonoBehaviour c in comps)
{
c.enabled = false;
}
GetComponent<TrackRenderer>().enabled = true;