- Home /
Basic object creation in arrays! Syntax error!
I'm trying to create a basic array of buildings, and store each Building object in an array slot. The array will hold a list of Building's that can later be put into a menu that would place the object in the world. If there is a better way to create an array to be accessed, besides a static class, which is storing all of my predetermined buildings, it would be awesome to learn!
Problem: The array of objects I am creating gives a syntax error saying it expects [ ] or ,
Aside from what I'm trying. This is what I have in mind for the game system I am making. The player opens a menu, picks a building and places a building. The buildings are predetermined and listed onto a
BUILDING OBJECT------------------------------------------------------------
using UnityEngine;
using System.Collections;
public class Building : MonoBehaviour {
private int curHealth;
private int maxHealth;
private bool isAlive;
public Building(int curHealth, int maxHealth, bool isAlive){
this.curHealth=curHealth;
this.maxHealth=maxHealth;
create();
}
public void destroy()
{
this.destroy();
}
public Building create()
{
isAlive=true;
return this;
}
public bool getAlive()
{
return isAlive;
}
public void hpUpdate()
{
if (curHealth<=0)
{
isAlive=false;
destroy ();
}
else{
curHealth+=2;
}
}
}
CLASS THAT STORES BUILDINGS---------------~ERROR IS IN THIS CLASS~----------------
using UnityEngine;
using System.Collections;
public static class BuildingList {
static Building[] buildings=new Building[3];
buildings[0]=new Building(100, 100, true);
buildings[1]=new Building(500, 500, true);
buildings[2]=new Building(50, 100, true);
static public Building[] getList()
{
return buildings;
}
}
MENU TO BUILD------------------------------------
using UnityEngine;
using System.Collections;
public class BuildMenu : MonoBehaviour {
Building[] temp;
// Use this for initialization
void Awake () {
Building[] temp=BuildingList.getList();
}
// Update is called once per frame
void Update () {
if(Input.GetButtonDown("Build"))
{
}
}
void FixedUpdate(){
for(int i=0;i<temp.Length;i++)
{
temp[i].hpUpdate();
}
}
}
Please copy the error message from the console and add it to your question.
Added a screenshot of the compiler errors I am currently receiving.
Answer by fafase · Mar 15, 2014 at 08:28 PM
You have a constructor in a MonoBehaviour class, Unity won't like it. You need to use AddComponent instead and remove the ctor.
I have not gone through the whole code you provide, instead I will explain how I will do it.
Create a manager game object, just an empty game object and add a script that is not static. The script has an array of building prefab:
public GameObject [] building;
this will show Building in your inspector, if you give a size greater than 0, you can drag and drop your prefab of building.
Then each of your GUI button could be given a integer value, this value will correspond to the index where the corresponding building is stored in the array.
Then you can instantiate like:
Instantiate(buildingManager.building[i], position, rotation);
Of course this requires that you have a reference to the BuildingManager done with GetComponent.
Somehow:
public class GUIClass:MonoBehaviour{
GameObject[]buildings;
void Start(){
buildings = bm.buildings;
}
int index
void OnGUI(){
for (int i = 0; i < bm.buildings.Length; i++){
if(GUI.Button(rect[i], buildings[i].name))index = i;
}
}
void PlaceBuilding(){
// Position comes from somewhere else
Instantiate(building[index],position, Quaternion.identity)
}
}
now the Building script:
public class BuildingManager:MonoBehaviour{
public GameObject[] buildings;
}
This is not guarantee to work but should lead you to the way.
Thank you! Working it out now, but it makes complete sense what to do! Should make the rest of my scripting a lot easier.