Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MiniForge · Mar 15, 2014 at 06:11 AM · c#gameobjectarraysmenu

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 ,

alt text

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();
             }
         }
     }
     


capture.jpg (83.3 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Mar 15, 2014 at 06:11 AM 0
Share

Please copy the error message from the console and add it to your question.

avatar image MiniForge · Mar 15, 2014 at 08:06 PM 0
Share

Added a screenshot of the compiler errors I am currently receiving.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MiniForge · Mar 15, 2014 at 11:35 PM 0
Share

Thank you! Working it out now, but it makes complete sense what to do! Should make the rest of my scripting a lot easier.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Menu, Game mode Array String to change settings? 1 Answer

Multiple Cars not working 1 Answer

Array of Arrays of GameObjects/Prefabs (C#) 1 Answer

How to merge an arrays into arrays inside an array? 3 Answers

Remove GameObject From Array if it already exists? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges