Search Unity

Help me to understand what is the problem with my script,pls.

Discussion in 'Scripting' started by Norgle, Aug 30, 2015.

  1. Norgle

    Norgle

    Joined:
    Aug 30, 2015
    Posts:
    2
    I'm new at C# and I don't understand what is the problem with my scripts. Sorry for my English. Unity throw an NullReferenceException:
    Object reference not set to an instance of an object
    Tile.Priority (System.String playerObject) (at Assets/My Files/Scripts/Tile.cs:18)
    UnityEngine.Component:BroadcastMessage(String, Object)
    ComputerTrigger:Start() (at Assets/My Files/Scripts/ComputerTrigger.cs:12)
    in these scripts:


    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Tile : MonoBehaviour {
    6.  
    7. [INDENT]   public Dictionary <string, int> priorityMap;[/INDENT]
    8.     private Collider[] coll;
    9.     private string playerObject;
    10.     private int priority = 0;
    11.     private static Dictionary <string, int> tmpMap;
    12.  
    13.     //Debug
    14.     public int Check;
    15.  
    16.     public void Priority(string playerObject){
    17.         priorityMap.Add (playerObject,0);
    18.         this.playerObject = playerObject;
    19.         for (int i = 0; i<4; i++) {
    20.             CheckTilesAround(i);
    21.         }
    22.     }
    23.     void getMap(){
    24.         tmpMap = priorityMap;
    25.     }
    26.  
    27.     void CheckTilesAround(int arg){
    28.         //0 - left, 1 - up, 2 - right, 3 - down (relative global coordinates)
    29.         lock (this) {
    30.             if (arg == 0) {
    31.                 coll = Physics.OverlapSphere (new Vector3 (gameObject.transform.position.x - 1f, gameObject.transform.position.y, gameObject.transform.position.z), 0.5f);
    32.                 if (coll != null) {
    33.                     for (int i = 0; i < coll.Length; i++) {
    34.                         if (coll .name == "Tile") {
    35.                             CheckPriority();
    36.                             getMap();
    37.                             coll.GetComponent<Tile>().BroadcastMessage("Priority",playerObject);
    38.                         }
    39.                     }
    40.                 }
    41.             } else if (arg == 1) {
    42.                 coll = Physics.OverlapSphere (new Vector3 (gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z + 1f), 0.5f);
    43.                 if (coll != null) {
    44.                     for (int i = 0; i < coll.Length; i++) {
    45.                         if (coll .name == "Tile") {
    46.                             CheckPriority();
    47.                             getMap();
    48.                             coll.GetComponent<Tile>().BroadcastMessage("Priority",playerObject);
    49.                         }
    50.                     }
    51.                 }
    52.             } else if (arg == 2) {
    53.                 coll = Physics.OverlapSphere (new Vector3 (gameObject.transform.position.x + 1f, gameObject.transform.position.y, gameObject.transform.position.z), 0.5f);
    54.                 if (coll != null) {
    55.                     for (int i = 0; i < coll.Length; i++) {
    56.                         if (coll .name == "Tile") {
    57.                             CheckPriority();
    58.                             getMap();
    59.                             coll.GetComponent<Tile>().BroadcastMessage("Priority",playerObject);
    60.                         }
    61.                     }
    62.                 }
    63.             } else if (arg == 3) {
    64.                 coll = Physics.OverlapSphere (new Vector3 (gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z - 1f), 0.5f);
    65.                 if (coll != null) {
    66.                     for (int i = 0; i < coll.Length; i++) {
    67.                         if (coll .name == "Tile") {
    68.                             CheckPriority();
    69.                             getMap();
    70.                             coll.GetComponent<Tile>().BroadcastMessage("Priority",playerObject);
    71.                         }
    72.                     }
    73.                 }
    74.             }
    75.         }
    76.     }
    77.     void CheckPriority(){
    78.         if (tmpMap != null) {
    79.             Dictionary<string,int>.Enumerator enumeratorTmp = tmpMap.GetEnumerator ();
    80.             while (enumeratorTmp.MoveNext()) {
    81.                 KeyValuePair<string,int> pairTmp = enumeratorTmp.Current;
    82.                 if (pairTmp.Key.Equals (playerObject))
    83.                     priority = pairTmp.Value + 1;
    84.             }
    85.         }
    86.         if (priorityMap.ContainsKey (playerObject)) {
    87.             Dictionary<string,int>.Enumerator enumerator = priorityMap.GetEnumerator ();
    88.             while (enumerator.MoveNext()) {
    89.                 KeyValuePair<string,int> pair = enumerator.Current;
    90.                 if (pair.Key.Equals (playerObject) && priority < pair.Value) {
    91.                     priorityMap.Remove (playerObject);
    92.                     priorityMap.Add (playerObject, priority);
    93.                     Check = priority;
    94.                 }
    95.             }
    96.         } else {
    97.             priorityMap.Add (playerObject, priority);
    98.             Check = priority;
    99.         }
    100.     }
    101. }

    and


    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ComputerTrigger : MonoBehaviour {
    5.  
    6.     private Collider[] tile;
    7.  
    8.     void Start () {
    9.         tile = Physics.OverlapSphere (new Vector3(gameObject.transform.position.x,gameObject.transform.position.y - 0.5f,gameObject.transform.position.z), 1f);
    10.         if(tile[0].name == "Tile"){
    11.             tile[0].GetComponent<Tile>().BroadcastMessage("Priority","Computer");
    12.  
    13.         }
    14.     }
    15.  
    16. }
    Please explain me what is the problem.

    P.s. Method BrodcastMessage create a new thread or continue current one?
     
    Last edited: Aug 30, 2015
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Code tags please.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Change line 7 to

    Code (csharp):
    1. public Dictionary <string, int> priorityMap = new Dictionary <string, int>();
    In general it's normally better to write scripts from scratch when you start off, not copy and paste stuff iff the internet.
     
    Norgle and Timelog like this.
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    GroZZleR likes this.
  5. Norgle

    Norgle

    Joined:
    Aug 30, 2015
    Posts:
    2
    Thank you very much, I knew adout it, but forget