I am trying to create a level start-up script that initialize default values for other components, such as its health, speed and stat, or the cost to build an unit
To keep it modular, I prefer not to define the default values in the Start() or Awake() methods, but to pass it in after reading from a XML file. However this means writing a script where I have to manually find all the components I wish to change.
I could do a delegate and store it in the component, and invoke it inside Awake() or Start(). The question is how do you set the callback before the component's Awake() is ran.
What are some other methods there are which I can be accomplish passing game data to components?
You need to create a prefab: An empty object with your script containing all variables that you want interact. In my case the prefab and the script have the same name : Variables_pass
In the Variables_pass script you need to put:
void Start()
{
DontDestroyOnLoad(gameObject);
}
In the first scene create another empty object and put the follow script:
using UnityEngine;
using System.Collections;
public class StartControl : MonoBehaviour {
public GameObject CommonVariables; // Attach the Varibles_pass prefab here.
void Awake()
{
if(!GameObject.Find("Variables_pass(Clone)")) // the Variables_pass(Clone) object have Don't destroy call, so you can call all public variables from anywhere "any scene".
Instantiate(CommonVariables); // instantiate just one time the variable passer (" prefab Variables_pass") .
}
}