Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Instantiating a Parent nd then instantiating a child into that parent

Discussion in 'Scripting' started by Jinngineer, Nov 17, 2021.

  1. Jinngineer

    Jinngineer

    Joined:
    Apr 22, 2020
    Posts:
    41
    Hello, as the title says im trying to instantiate a parent object, then instantiating a grid of child objects into the parent object.

    I looked online for results and i think i found one but then i got a NullReference Exception.

    The result i found on the forumns and it worked for others but im curiou why it didnt work in my script.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class GameManager : MonoBehaviour
    7. {
    8.     [SerializeField]
    9.     GameObject cube, cube2, grid ;
    10.  
    11.     Vector3 position = new Vector3(0f, 0f, 0f);
    12.  
    13.     [SerializeField]
    14.     Rect board;
    15.  
    16.     GameObject cBoard;
    17.  
    18.     void Start()
    19.     {
    20.         GameBoard();
    21.          cBoard = Instantiate(grid, position, Quaternion.identity);
    22.     }
    23.  
    24.     void GameBoard()
    25.     {      
    26.             for(int i=0; i < board.x; i=i+1)
    27.             {
    28.                 position.x = i;
    29.  
    30.                 for(int o =0; o< board.y; o = o + 1)
    31.                 {
    32.                 position.z = o;
    33.  
    34.                 CheckerBoard();
    35.                 }                                        
    36.             }                
    37.     }
    38.  
    39.     void CheckerBoard()
    40.     {      
    41.         if (position.x%2 == 0)
    42.         {
    43.             if(position.z%2 == 1)
    44.             {
    45.                 GameObject.Instantiate(cube, position, Quaternion.identity, cBoard.transform);
    46.                
    47.             }
    48.             else if(position.z % 2 == 0)
    49.             {
    50.                 GameObject.Instantiate(cube2, position, Quaternion.identity, cBoard.transform);
    51.                
    52.             }
    53.          
    54.         }
    55.  
    56.         else if (position.x % 2 == 1)
    57.         {
    58.             if (position.z % 2 == 0)
    59.             {
    60.                 GameObject.Instantiate(cube, position, Quaternion.identity, cBoard.transform);
    61.  
    62.             }
    63.  
    64.             else if (position.z % 2 == 1)
    65.             {
    66.                 GameObject.Instantiate(cube2, position, Quaternion.identity, cBoard.transform);
    67.  
    68.             }
    69.  
    70.         }
    71.  
    72.     }
    73.  
    74.  
    75.  
    76. }
    77.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    The answer is always the same... ALWAYS... and it doesn't even matter what you're doing. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

    - drag it in using the inspector
    - code inside this script initializes it
    - some OTHER external code initializes it
    - ? something else?

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.

    Here is a clean analogy of the actual underlying problem of a null reference exception:

    https://forum.unity.com/threads/nul...n-instance-of-an-object.1108865/#post-7137032
     
    Jinngineer likes this.
  3. Jinngineer

    Jinngineer

    Joined:
    Apr 22, 2020
    Posts:
    41
    NullReference Error has been fixed... i now instantiate the cubes and the grid Game object into the scene.... but the Cubes are not the children of the game objects. from what i understand, this is the format that Unity API suggests to do that. Though im prob misreading it xD

    @Kurt-Dekker Thanks for the Assist with the null reference :) . You info was 30 seconds to late xD, i had figured out the null reference right before i checked this post again.


    Code (CSharp):
    1.  
    2.  
    3. public class GameManager : MonoBehaviour
    4. {
    5.     [SerializeField]
    6.     GameObject cube, cube2, grid;
    7.  
    8.     Vector3 position = new Vector3(0f, 0f, 0f);
    9.  
    10.     [SerializeField]
    11.     Rect board;
    12.  
    13.     Transform cBoard;
    14.  
    15.     void Start()
    16.     {
    17.         GameBoard();
    18.          cBoard = Instantiate(grid.transform, position, Quaternion.identity);
    19.     }
    20.  
    21.     void GameBoard()
    22.     {      
    23.             for(int i=0; i < board.x; i=i+1)
    24.             {
    25.                 position.x = i;
    26.  
    27.                 for(int o =0; o< board.y; o = o + 1)
    28.                 {
    29.                 position.z = o;
    30.  
    31.                 CheckerBoard();
    32.                 }                                        
    33.             }                
    34.     }
    35.  
    36.     void CheckerBoard()
    37.     {      
    38.         if (position.x%2 == 0)
    39.         {
    40.             if(position.z%2 == 1)
    41.             {
    42.                 GameObject.Instantiate(cube, position, Quaternion.identity, cBoard);
    43.                
    44.             }
    45.             else if(position.z % 2 == 0)
    46.             {
    47.                 GameObject.Instantiate(cube2, position, Quaternion.identity, cBoard);
    48.                
    49.             }
    50.          
    51.         }
    52.  
    53.         else if (position.x % 2 == 1)
    54.         {
    55.             if (position.z % 2 == 0)
    56.             {
    57.                 GameObject.Instantiate(cube, position, Quaternion.identity, cBoard);
    58.  
    59.             }
    60.  
    61.             else if (position.z % 2 == 1)
    62.             {
    63.                 GameObject.Instantiate(cube2, position, Quaternion.identity, cBoard);
    64.  
    65.             }
    66.  
    67.         }
    68.  
    69.     }
    70.  
    71.  
    72.  
    73. }
    74.