using UnityEngine;
using System.Collections;
public class gridManager : MonoBehaviour {
bool blahblah = false;
public GameObject[,] world;
public void addToWorld (int x, int y, string type){
//if the array position is not empty
if (world [x,y] != null) {
GameObject n = (GameObject)Instantiate(Resources.Load(type));
//tell the object where is is
n.transform.position = new Vector2(x*10,y*10);
world [x,y] = n;
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (blahblah == false) {
addToWorld (2,2,"Test Object");
blahblah = true;
}
}
}
I assume its not finding the “Test Object” prefab?
What I’m trying to do is Instantiate a prefab with ‘guidance’ from a grid manager. For now I’m trying to check if the array position is empty. If it is, instantiate the “Test Object” prefab at the given x,y coords (*10, because grid size ofc.) Then put a reference to the game object in the array so I can check if from other objects later, by position in the array (and by extension in the game world.)
Any help would be great.
Halbera.
EDIT:
NullReferenceException: Object reference not set to an instance of an object
gridManager.addToWorld (Int32 x, Int32 y, System.String type) (at Assets/gridManager.cs:12)
gridManager.Update () (at Assets/gridManager.cs:28)