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 fighder · Feb 22, 2015 at 12:06 PM · gameobjectnullreferenceexceptiongrid based game2d array

How to add a gameobject to a null gameobject

The question may seem confusing but I can't think of another way to ask it about it. So I have a project in cloning TWO DOTS the game. So I am making a grid system and I'm trying to assign the circles as game objects onto each grid. The following script creates the grid.

 using UnityEngine;
 using System.Collections;
 
 public class gridManager : MonoBehaviour
 {
         public int width;
         public int height;
         public GameObject[] prefabArray;
         private infoClass[] gridInfo1D;
         private infoClass[,] gridInfo2D;
         public float circleRad;
         //private circleProperty[] circles;
 
         void Start ()
         {
                 gridInfo1D = Initialize (width * height);
                 gridInfo2D = Converter (gridInfo1D, width, height);
 
                 for (int row = 0; row < gridInfo2D.GetLength(0); row++) {
                         for (int column = 0; column < gridInfo2D.GetLength(1); column++) {
                                 GameObject tempCircle = (GameObject)Instantiate (prefabArray [Random.Range (0, prefabArray.Length)], transform.position, Quaternion.identity);
                                 gridInfo2D [row, column].circle = tempCircle;
                                 gridInfo2D [row, column].SendMessage ("SetPos", new Vector2 (row * circleRad, column * circleRad));
 
                         }
                 }
 
         }
 
         infoClass[] Initialize (int size)
         {
                 infoClass[] array = new infoClass[size];
                 return array;
         }
 
         infoClass[,] Converter (infoClass[] array1D, int width, int height)
         {
                 infoClass[,] array2D = new infoClass[width, height];
                 
                 for (int i=0; i<array1D.Length; i++) {
                         int row = i / width;
                         int column = i % width;
                         array2D [row, column] = array1D [i];
                 }
                 return array2D;
         }
 
 }
 

The next script below is the actual class for the grid.

 using UnityEngine;
 using System.Collections;
 
 public class infoClass : MonoBehaviour
 {
 
 
         public string color;
         public bool occupyCheck;
         public GameObject circle;
 
         void Update ()
         {                
                 if (circle) {
                         occupyCheck = true;        
                         color = circle.tag;
                 } else {
                         occupyCheck = false;        
                 }
         }
 
         void SetPos (Vector2 xy)
         {
                 float x = xy.x;
                 float y = xy.y;
                 circle.transform.position = new Vector3 (x, y);
         }
 }
 

The problem is that when I run the code, it gives me a null reference error when I try to assign tempCircle to circle.

 gridInfo2D [row, column].circle = tempCircle;

I understand when I first initialized the gridInfo2D the circle object doesn't exist yet, but is there a way where I can replace that null with an actual game object later?

Comment
Add comment · Show 1
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 fighder · May 08, 2015 at 12:32 AM 0
Share

solved, please remove.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Feb 22, 2015 at 03:39 PM

This is a regular C# problem. Nothing do to with gameObjects. Read a little more, just from a general site, about making arrays of classes. The array AND everything inside needs to be new'ed.

In other words, if you had a single infoClass A;, you'd need A=new infoClass();. If you have 1000 of them in an array, you don't get free stuff. You still need to NEW all 1000 of them.

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 fighder · Feb 22, 2015 at 07:44 PM 0
Share

if i do a for statement to NEW them is it possible?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Select every GameObject in GameObject Array 2 Answers

NullReferenceException on Public Variable only Outside the Class 2 Answers

How to Check if a component Exists on a Gameobject. 3 Answers

Save a precalculated grid path into a jagged 2d array ready to be loaded when need 0 Answers

Resources.LoadAll(); GameObject[] Array 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