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

NullReference help

Discussion in 'Scripting' started by equal, Apr 6, 2014.

  1. equal

    equal

    Joined:
    Dec 14, 2012
    Posts:
    77
    Hi,

    i have this code here:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class terrainCollumn{
    5.  
    6.     public Object block;
    7.  
    8.     public List<GameObject> blocks;
    9.    
    10.    
    11.    
    12.     //public int CollumnHeight = 50;
    13.  
    14.     // Use this for initialization
    15.     public terrainCollumn () {
    16.        
    17.         block = Resources.Load("Block");
    18.    
    19.     }
    20.    
    21.    
    22.     public int init(int gainedPoint, int gainedCollumnNumber)
    23.     {
    24.         for (int i = 0; i < gainedPoint; i++)
    25.         {
    26.             GameObject newBlock = MonoBehaviour.Instantiate(block,new Vector3((float)gainedCollumnNumber/4,(float)i/4,0), Quaternion.identity) as GameObject;
    27.             MonoBehaviour.print(newBlock.GetType());
    28.             blocks.Add(newBlock);
    29.         }
    30.        
    31.         return gainedPoint + Random.Range(1,-2);
    32.     }
    33.    
    34.  
    35. }
    36.  
    But no matter how i turn it around it just stops at the blocks.Add(newBlock);

    I tried to double and tripple check if the type is the needed type, and i should not have a nulref because i can obviously get the name/type in just 1 line before.

    Greets mike
     
  2. primaerfunktion

    primaerfunktion

    Joined:
    Jan 16, 2012
    Posts:
    98
    You have to create the List first by using "blocks = new List<GameObject>()" inside your start function or wherever.
     
  3. equal

    equal

    Joined:
    Dec 14, 2012
    Posts:
    77
    I feel so retarded now. wow...

    Thanks man!!
    ( , mb i programed egnouth today)
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Could you explain line 27?
     
  5. equal

    equal

    Joined:
    Dec 14, 2012
    Posts:
    77
    @Dantus

    As this class is not inherited from Monobehaviour, in order to still use the print, i have to access that static print function with a explicit Monobehaviour.print() call.
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Interesting, I wasn't even aware that Unity has a lower case static method call in MonoBehaviour!? I though only Debug.Log existed for that.