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

What The BuhJeebers?!?!

Discussion in 'Scripting' started by renman3000, Jul 28, 2014.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    Hi there,
    Sorry for posting this as it seems absurd I can not firgure this out, but I am continaully getting a NullExceptionReference.

    I have a List of roads (road_script), in a roadsMng. The roadsMng at start asks roads[0], to perform a task. The road does, however I immediately get a message back of null reference exception. I have not removed or asked anything else to happen.

    Scratching my head.


    Code (csharp):
    1.  
    2. public List<roadsScript> roads;
    3. ///roads mng
    4. void Start(){
    5. roads[0].doThis();
    6. }
    7.  
    Code (csharp):
    1.  
    2. //road script
    3. public void doThis(){
    4. //this road does this.
    5. }
    6.  
    I then get the error referencing the line 1 of Start in the roads mng.


    ????!!!***
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    did you initialise the roads array to contain "things"? i.e. some sort of for loop putting something in each array "slot"? or did you just declare that an array of "slots for things" exist?

    If the slots haven't been filed with something trying to call a function from something that isn't there will cause this error message.
     
  3. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    They are all filled, it is a list, not an array.
     
  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Array or list... same thing. You did put something in it?
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    can we see the code that puts things in the list?
     
  6. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    Code (csharp):
    1.  
    2. ///roads mng, prints all return correct values.
    3.     void Start(){
    4.         print("roads[0] " + roads[0].transform.name);
    5.         print("roads[1] " + roads[1].transform.name);
    6.         print("roads[2] " + roads[2].transform.name);
    7.         print("roads[3] " + roads[3].transform.name);
    8.         print("stat node " + StartNode.name);
    9.         roads[0].getRoad(StartNode);
    10.     }
    11.  
    Code (csharp):
    1.  
    2. ///road script...
    3.     public void getRoad(Transform lastRoadEndNodeDel){
    4.         ///this road section is loaded....
    5.          
    6.         //set parent
    7.         transform.parent = moverTransform;
    8.      
    9.         //remove from list
    10.         RoadsMng_Data.roads.RemoveAt(0);
    11.      
    12.      
    13.         ///place start point at last road end point
    14.         transform.localPosition = lastRoadEndNodeDel.localPosition;
    15.      
    16.      
    17.         ///movement handled by the main roads mng, player controller
    18.      
    19.      
    20.         //report back to roads mng, of this end point
    21.         RoadsMng_Data.endNode = endNode;  
    22.     }
    23.  


    ??
     
  7. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    AH HAH!!!
    So Sorry,
    I had duplicated the mng, to use as another transform and forgot to remove the mng component.

    #Monday