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 SarahWIld · Oct 27, 2012 at 12:43 AM · gameobjectarray

Array of GameObjects created with a Script

I am trying to create a 2D array of game objects with a script, so upon initialization I get an array of a million or so objects. I C++ this is piss easy, however in Unity it just does not work. No matter what I look at nothing works. I have something I am working but I have no idea on how to create an array of objects. The entire game I'm making will all be in Script form. I don't want to hard code a million or more nodes. I have only done a 100*100 array for now. From this code I get 1 node in centre of screen.

 using UnityEngine;
 using System.Collections;
 
 public class Gird_Grid : MonoBehaviour {
 
     [System.Serializable]
     public class Gird_Node{
         public GameObject Node = new GameObject();
     };
     public GameObject pPrefab;
     public Gird_Node[,] Gird_Array = new Gird_Node[100,100];
     
     // Use this for initialization
     void Start () {
         int count = 0;
         for( int i = 0; i < 100; i++)
         {
             for( int j = 0; j < 100; j++)
             {
                 Gird_Array[i,j].Node = Instantiate(pPrefab, new Vector3(i*1.0F,j*1.0F,0.0F), Quaternion.identity) as GameObject;
                 count++;
                 //Gird_Array[i,j].Node.transform.position = new Vector3(i, j, -1);
                 
             }
         }
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }
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 whydoidoit · Oct 27, 2012 at 04:27 AM 0
Share

You need to allocate the member of Gird_Array as well as the space for the array presu$$anonymous$$g the thing in the array is a class and not a structure.

      Gird_Array[i,j] = new Gird_Node();
      Gird_Array[i,j].Node = Instantiate(pPrefab,...
avatar image whydoidoit · Oct 27, 2012 at 04:27 AM 0
Share

Pretty sure that's the same in C++ right?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DaveA · Oct 27, 2012 at 12:46 AM

A million is a lot to expect, good luck.

That said, try using float instead of int for i and j, or cast differently:

 new Vector3((float)i,(float)j,0.0f)


and you don't need to initialize like this, just let it be null cuz it will be overwritten anyway and it's usually bad practice to use 'new' in constructors, do 'new' in Start if possible

 public GameObject Node; //that's good enough     = new GameObject();
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 SarahWIld · Oct 27, 2012 at 01:35 AM 0
Share

Instantiate(pPrefab, new Vector3((float)i*1.0F,(float)j*1.0F,0.0F), Quaternion.identity); works perfectly, but I want this to be a gameobject in a C# class or array and don't care which as I need to assign separate params to each node.

having just

Instantiate(pPrefab, new Vector3((float)i*1.0F,(float)j*1.0F,0.0F), Quaternion.identity);

is of no use to me.

avatar image SarahWIld · Oct 27, 2012 at 01:36 AM 0
Share

Still nothing, I get

NullReferenceException: Object reference not set to an instance of an object on line Gird_Array[i,j].Node = Instantiate(pPrefab, new Vector3(i*1.0F,j*1.0F,0.0F), Quaternion.identity) as GameObject;

Why does none of the other help questions I look at work. Id be better off writing my own game engine for android in C++, at least C++ works unlike java and C#

avatar image aldonaletto · Oct 27, 2012 at 02:23 AM 0
Share

You can't handle 1 million game objects in Unity: at most 64$$anonymous$$, but not at practical frame rates. To make something usable in a desktop you should work with 1000 or less game objects - a lot less, if you're developing for Android devices...
That's not an Unity fault: allocating and managing 1 million memory blocks is extremely slow in any system or language.

avatar image
0

Answer by SarahWIld · Oct 27, 2012 at 04:13 AM

GameObject Nodes[i,j] = Instantiate(pPrefab, new Vector3((float)i*1.0F,(float)j*1.0F,0.0F), Quaternion.identity) as GameObject;

This works, but with a class, it don't. So I will have to have a Class[,] and GameObject[,] Separate. Why is Unity so crap as the most basic function levels. This surely cane be how shit C# is.

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 whydoidoit · Oct 27, 2012 at 04:24 AM 0
Share

Ok - so why do you want these things to be game objects? If you really have 1m independent game objects then there is going to be a massive overhead - a game object is effectively going to be a draw call (though batching might reduce it). Your game model sounds like it needs to be manipulating your own classes rather than prefabs and perhaps constructing meshes that can be moved.

You are wrestling with two things here - one is the switch from C++ to C# - C# has no problem making a 1m instances of a class.

        public class SomeClass
        {
               public string someString = string.Empty;
               public object someObject;
        }  

        SomeClass[,] array = new SomeClass[1000,1000];
   
        for(var i = 0; i < 1000; i++)
        {
              for(var j = 0; j < 1000; j++)
              {
                    array[i,j] = new SomeClass();
              }
        }

But you are trying to make part of the Unity framework with a GameObject - a prefab is a heavy weight thing, it probably has a renderer, an animation etc etc - Unity as a framework isn't designed to have 1m of those - it just isn't going to work.

Your best bet would be to create your own system within Unity to handle the game design you are after and benefit from all of the places where Unity will help you. $$anonymous$$oving 1m things using GameObjects isn't the design objective in Unity - but, for example, you could have a whole bunch of bones that moved parts of a mesh.

At the end of the day all systems have a basic design that they are scoped to produce, it is certainly possible that Unity isn't for you on this project.

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

12 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

Related Questions

Is this doable in unity 2 Answers

Access variables, objects in array in another script? 2 Answers

Array problem -3 Answers

Access a String array in one script from another script 0 Answers

how to build & populate an array available to all scripts on all objects? 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