Last active
September 21, 2017 22:20
-
-
Save ProfPollati/47b7a6ced09f411adab3095a36b25bfe to your computer and use it in GitHub Desktop.
Unity C# - Add a script to each child on startup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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