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 NickP_2 · Sep 04, 2013 at 05:07 PM · gameobjectgenerationminecraftmono-behaviour

Error: trying to create a MB using "new" keyword...

I'm trying to generate a minecraft look-alike terrain.

Here's the supperclass/sprite to create a block:

 private GameObject blockObject;
     
     protected string name;
     protected float size;
     protected Color blockColor;
     protected Vector3 position;
 
     ...

     public void Place(Vector3 blockPos)
     {
         position = blockPos;
         
         blockObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
         blockObject.renderer.material.color = blockColor;
         blockObject.transform.localScale = new Vector3(size, size, size);
         blockObject.transform.position = position;
     }

An example of a GrassBlock:

 public class GrassBlock : BlockSprite 
 {
     public GrassBlock()
     {
         name = "grassBlock";
         size = 1;
         blockColor = Color.green;
     }
 }

And now I want to generate a Chunk, when the "A" button is pressed:

 private BlockSprite[, ,] blocks;
 
 private GrassBlock grassBlock = new GrassBlock();
 private DirtBlock dirtBlock = new DirtBlock();
 
 public int chunkSize = 10;

 public int ChunkSize {
     get {
         return this.chunkSize;
     }
     set {
         chunkSize = value;
     }
 }
 public ChunkGenerator()
 {
     blocks = new BlockSprite[10, 10, 10];
     GenerateTopChunk();
 }
 
 public void GenerateTopChunk()
 {
     for(int y = 0; y < chunkSize; y++)
     {
         for(int z = 0; z < chunkSize; z++)
         {
             for(int x = 0; x < chunkSize; x++)
             {
                 if(y < chunkSize - 1)
                 {
                     blocks[x, y, z] = new DirtBlock();
                 }
                 else
                 {
                     blocks[x, y, z] = new GrassBlock();
                 }
             }
         }
     }
 }
     public void PlaceBlocks(Vector3 position)
 {
     for(int y = 0; y < ChunkSize; y++)
     {
         for(int z = 0; z < chunkSize; z++)
         {
             for(int x = 0; x < chunkSize; x++)
             {
                 blocks[x, y, z].Place(new Vector3(x, y, z));
             }
         }
     }
 }
 void Update()
 {
     if(Input.GetKeyDown(KeyCode.A))
     {
         PlaceBlocks(new Vector3(0, 0, 0));
     }
 }

But whenever I start the game I get 999+ warnings: You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all UnityEngine.MonoBehaviour:.ctor() BlockSprite:.ctor() DirtBlock:.ctor()

So... I'm stuck :O

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 MantisFRK · Sep 05, 2013 at 02:26 PM 0
Share

Oy! Have you tried to remove the monoBehaviour inheritance?

avatar image NickP_2 · Sep 05, 2013 at 03:19 PM 0
Share

Yes, that did the trick, found it out at the same time you commented :) thanks !

2 Replies

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

Answer by flaviusxvii · Sep 05, 2013 at 02:23 PM

This won't work if GrassBlock inherits from MonoBehaviour

 private GrassBlock grassBlock = new GrassBlock();

You'll want to declare it like this:

 private GrassBlock grassBlock = null;

And then in your Start() function do this:

 grassBlock = gameObject.AddComponent<GrassBlock>();



NOTE: If you are implementing minecraft-like terrain you won't be able to have a gameObject for every cube of the terrain. Be warned.

Comment
Add comment · Show 3 · 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 NickP_2 · Sep 05, 2013 at 03:00 PM 0
Share

I also solved the problem with disabling the blocksprite to inherit from $$anonymous$$onogBehavior. And please, tell me more about your "NOTE" ! :)

avatar image flaviusxvii · Sep 05, 2013 at 06:01 PM 0
Share

Well, as you can imagine you're not the first person do try something like $$anonymous$$ecraft in Unity. If you google "voxel terrain unity" or "$$anonymous$$ecraft unity" you'll see lots of videos and tutorials about it.

Ultimately they end up using one gameObject to represent dozens or hundreds of terrain blocks, by dynamically generating a mesh.

avatar image NickP_2 · Sep 05, 2013 at 10:26 PM 0
Share

Thanks for the respond, voxel terrain is what I was looking for!

avatar image
0

Answer by Seizure · Sep 04, 2013 at 05:11 PM

 public ChunkGenerator()
 {
     blocks = new BlockSprite[10, 10, 10];
     GenerateTopChunk();
 }
 
 //should probably be
 
 public void ChunkGenerator()
 {
     blocks = new BlockSprite[10, 10, 10];
     GenerateTopChunk();
 }
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 NickP_2 · Sep 05, 2013 at 02:18 PM 0
Share

No, Its a constructor, not a method, and this wouldn't solve anything I guess (its double code)... Thanks tho :)

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

19 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

Related Questions

How does rycast work? 2 Answers

Adding gravity and texture to newly added game objects 1 Answer

Generate objects around object based on bounding size 0 Answers

World Generation with Interesting Terrain 0 Answers

Randomly generating Character Controllers? 0 Answers


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