- Home /
This question was
closed Mar 15, 2015 at 07:04 PM by
Graham-Dunnett for the following reason:
Duplicate Question
Question by
skeetninjer · Mar 15, 2015 at 07:03 PM ·
scripting problemnullreferenceexception
Problem accessing public method of script attached to gameobject.
Hello. I'm trying to access a public method from a script attached to a gameobject. Whenever I try to run the code inside of update in GameManager I get a NullReferenceException: Object reference not set to an instance of object. I'm not really sure whats going on here. Anyone have an idea?
public class GameManager : MonoBehaviour {
private UnitManager unitMan;
// Use this for initialization
void Start () {
...
unitMan = new UnitManager( playerPos, aiPos, AIUnitPrefab, UnitPrefab );
...
}
// Update is called once per frame
void Update () {
unitMan.curUnit.GetComponent<Unit>().updatePos();
}
}
public class UnitManager { public List playerUnits = new List(); public List aiUnits = new List(); public Transform curUnit; public int test;
public UnitManager( List<Vector3> playerPos, List<Vector3> aiPos, Transform UnitPrefab, Transform AIUnitPrefab )
{
genUnits( playerPos, aiPos, UnitPrefab, AIUnitPrefab );
curUnit = playerUnits[0];
}
private void genUnits( List<Vector3> playerPos, List<Vector3> aiPos, Transform UnitPrefab, Transform AIUnitPrefab )
{
for (int i = 0; i < playerPos.Count; i++) {
playerUnits.Add (Transform.Instantiate (UnitPrefab));
playerUnits[i].position = playerPos[i];
}
for (int i = 0; i < aiPos.Count; i++) {
aiUnits.Add (Transform.Instantiate (AIUnitPrefab));
aiUnits[i].position = aiPos[i];
}
}
}
Comment