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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

NullReferenceException: Object reference not set to an instance of an object CreationSphère.Update

Discussion in 'Scripting' started by Nasatoe, Dec 27, 2015.

  1. Nasatoe

    Nasatoe

    Joined:
    Dec 23, 2015
    Posts:
    2
    Hello everyone ! First, I'm French so sorry for my english.
    I started to use Unity last week so i'm a debutant.

    I'm trying to do a Tic-Tac-Toe in 3D. I have a code in C# to the console and I want to use the same to Unity.
    Here, I try to print my array.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CreationSphère : MonoBehaviour
    5. {
    6.  
    7.     public GameObject Boule;
    8.     public GameObject Cube;
    9.  
    10.  
    11.     void Start()
    12.     {
    13.  
    14.     }
    15.  
    16.    
    17.     void Update()
    18.     {
    19.         if (Input.GetKey(KeyCode.Space))
    20.         {
    21.             int[][] matrix = new int[3][];
    22.             matrix[0][1] = 1;
    23.             matrix[1][2] = 2;
    24.             print_matrix(matrix, Boule, Cube); //My test.
    25.         }
    26.  
    27.     }
    28.  
    29.  
    30.     void print_matrix(int[][] array, GameObject rond, GameObject croix) //Croix = player 2
    31.     {
    32.  
    33.         int scare = array.Length;
    34.         for (int i = 0; i < scare; i++)
    35.         {
    36.             int a = 5;
    37.             for (int j = 0; j < scare; j++)
    38.             {
    39.                 int b = 5;
    40.                 Vector3 position = new Vector3(a, 0, b);
    41.                 switch (array[i][j])
    42.                 {
    43.                     case 1:
    44.                         Instantiate(rond, position, Quaternion.identity);
    45.                         break;
    46.  
    47.                     case 2:
    48.                         Instantiate(croix, position, Quaternion.identity);
    49.                         break;
    50.  
    51.                     case 0:
    52.                         break;
    53.                 }
    54.                 b += 10;
    55.  
    56.             }
    57.             a += 10;
    58.         }
    59.     }
    60.  
    61. }
    And the error is enclosed.

    Thanks for your help and sorry for my english.
     

    Attached Files:

  2. NoCakeNoCode

    NoCakeNoCode

    Joined:
    Aug 27, 2015
    Posts:
    21
    Hi , try to change the array codes like this:

    Code (CSharp):
    1. int[,] matrix = new int[3,3];
    2.             matrix[0,1] = 1;
    3.             matrix[1,2] = 2;
    The rest will need changing as well.
     
  3. Nasatoe

    Nasatoe

    Joined:
    Dec 23, 2015
    Posts:
    2
    Yeah my bad, I wrote my test like a two-dimensionnal array and not a jagged array.
    Here there is my correction for a jagged array:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (Input.GetKey(KeyCode.Space))
    4.         {
    5.             int[][] array = init_matrix(3);
    6.             array[1][0] = 1;
    7.             array[0][2] = 2;
    8.             print_matrix(array, Boule, Cube);
    9.         }        
    10.  
    11.     }
    12.  
    13.  
    14.     static int[][] init_matrix(int n)
    15.     {
    16.         int[][] matrix = new int[n][];
    17.         for (int x = 0; x < matrix.Length; x++)
    18.         {
    19.             matrix[x] = new int[n];
    20.         }
    21.         return matrix;
    22.     }
    Thanks for your help.
    Do you know if there is a débogueur for Unity ?