- Home /
Question by
Chocolade · Feb 27, 2017 at 03:44 PM ·
c#scripting problemscript.
How can i attach a script to many gameobject in array ?
In one script i have array of gameobjects
void Start()
{
waypoints = GameObject.FindGameObjectsWithTag("ClonedObject");
foreach(GameObject go in waypoints)
{
SphereCollider sc = go.AddComponent<SphereCollider>() as SphereCollider;
sc.isTrigger = true;
}
originalPosition = myTransform.position;
}
waypoints is array of GameObject
Now i have this script too
using UnityEngine;
class SlowDown : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
Debug.Log("Object " +other.name+" entered "+ name);
WayPoints.SetSpeed(WayPoints.moveSpeedSlowed);
}
private void OnTriggerExit(Collider other)
{
Debug.Log("Object " + other.name + " exited " + name);
WayPoints.SetSpeed(WayPoints.moveSpeed);
}
}
I want to attach this script to each GameObject in the waypoints array.
Comment
Best Answer
Answer by ASPePeX · Feb 27, 2017 at 03:59 PM
Just like you did it with the SphereCollider:
foreach(GameObject go in waypoints)
{
go.AddComponent<SlowDown>();
}