Skip to content

Instantly share code, notes, and snippets.

@ProfPollati
Last active September 21, 2017 22:20
Show Gist options
  • Save ProfPollati/47b7a6ced09f411adab3095a36b25bfe to your computer and use it in GitHub Desktop.
Save ProfPollati/47b7a6ced09f411adab3095a36b25bfe to your computer and use it in GitHub Desktop.
Unity C# - Add a script to each child on startup
using UnityEngine;
public class AddScriptToEachChild : MonoBehaviour {
public float changeValueOfScript = 10.0f;
void Start () {
foreach (Transform child in transform) {
AttachScript(child.gameObject);
}
}
void AttachScript(GameObject target) {
target.AddComponent<SomeOtherScript>();
SomeOtherScript sos = target.GetComponent<SomeOtherScript>();
sos.valueOfScript = changeValueOfScript;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment