- Home /
Question by
alexkaskasoli · Mar 26, 2014 at 01:28 AM ·
c#getcomponentgetcomponents
Getting all scripts attached to a gameObject (Problem)
GameObject go = GameObject.Instantiate(bullet, transform.position, Quaternion.identity) as GameObject;
Component[] c = go.GetComponents(MonoBehaviour);
foreach (MonoBehaviour script in c) {
script.direction = transform.position - target.transform.position;
}
I'm trying to access all the scripts attached on the game object I just instantiated. I get an error on the second line: "Expression denotes a type where a variable, value or method group was expected.
From what I can understand that should be the proper way to use GetComponents() but the C# documentation on rarely used methods is quite sparse.
Comment
Best Answer
Answer by Simon-Larsen · Mar 26, 2014 at 01:41 AM
Your syntax is mixed. I guess you may already have looked at this example https://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponents.html
What you're doing is mixing Javascript and C#
Change line 3 from:
Component[] c = go.GetComponents(MonoBehaviour);
to:
Component[] c = go.GetComponents<MonoBehaviour>();