Search Unity

Question Creating enemies from spawn points in a JSON list

Discussion in 'Scripting' started by rstump, Feb 3, 2023.

  1. rstump

    rstump

    Joined:
    May 31, 2017
    Posts:
    2
    I'm new to C# and programming in general so bear with me. I'm trying to spawn enemies at coordinates based on a json list. I've made it work with one enemy but I want more enemies to be created based on the number of positions in the json file.

    When I run this in Unity I get this error:
    NullReferenceException: Object reference not set to an instance of an object
    JsonUtils.Start () (at Assets/Scripts/JsonUtils.cs:38)

    I've ran some debug lines before that line but I'm not quite sure what I'm looking at or how to fix it. I don't think it's parsing the json file correctly is my best guess.

    Here's the JSON list:
    Code (csharp):
    1. {
    2.   "positions":
    3.   [
    4.   [-151, 0.95, 133],
    5.   [4, 5, 6]
    6.   ]
    7. }
    I have a JsonUtils.cs script attached to an empty 'adversaryManager' object in Unity:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Vector3Serialization
    6. {
    7.     public float x;
    8.     public float y;
    9.     public float z;
    10. }
    11.  
    12. public class Vector3Positions
    13. {
    14.     public float[][] positions;
    15. }
    16.  
    17.  
    18. public class JsonUtils : MonoBehaviour
    19. {
    20.     public static TextAsset jsonAsset;
    21.     public static string json;
    22.  
    23.     public static Vector3Positions vector3Positions;
    24.     public static List<Vector3> positions = new List<Vector3>();
    25.  
    26.     private void Start()
    27.     {
    28.         jsonAsset = Resources.Load("adversaryPOS") as TextAsset;
    29.         json = jsonAsset.text;
    30.         vector3Positions = JsonUtility.FromJson<Vector3Positions>(json);
    31.  
    32.         Debug.Log("JSON contents: " + json);
    33.         Debug.Log("vector3Positions: " + vector3Positions);
    34.         Debug.Log("vector3Positions.positions: " + vector3Positions.positions);
    35.  
    36.         if (vector3Positions != null)
    37.         {
    38.             foreach (float[] position in vector3Positions.positions)
    39.             {
    40.                 positions.Add(new Vector3(position[0], position[1], position[2]));
    41.             }
    42.         }
    43.         else
    44.         {
    45.             Debug.LogError("vector3Positions is null");
    46.         }
    47.     }
    48. }
    I also have an AdversaryManager.cs script attached to that same gameobject. In that script I have a prefab attached to the GameObject 'adversaryPrefab'.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AdversaryManager : MonoBehaviour
    6. {
    7.     public GameObject adversaryPrefab;
    8.  
    9.  
    10.     private List<Vector3> positions;
    11.  
    12.     void Start()
    13.     {
    14.         positions = JsonUtils.positions;
    15.         SpawnAdversaries1(positions);
    16.     }
    17.  
    18.     private void SpawnAdversaries1(List<Vector3> positions)
    19.     {
    20.         Debug.Log("Positions count: " + positions.Count);
    21.         foreach (Vector3 position in positions)
    22.         {
    23.             if (position != null)
    24.             {
    25.                 GameObject go = Instantiate(adversaryPrefab, position, Quaternion.identity);
    26.                 go.transform.parent = transform;
    27.             }
    28.             else
    29.             {
    30.                 Debug.LogError("Position is null");
    31.             }
    32.         }
    33.     }
    34. }
    Here's what my debug log says:
    JSON contents: {
    "positions":
    [
    [-151, 0.95, 133],
    [4, 5, 6]
    ]
    }
    UnityEngine.Debug:Log (object)
    JsonUtils:Start () (at Assets/Scripts/JsonUtils.cs:32)

    vector3Positions: Vector3Positions
    UnityEngine.Debug:Log (object)
    JsonUtils:Start () (at Assets/Scripts/JsonUtils.cs:33)

    vector3Positions.positions:
    UnityEngine.Debug:Log (object)
    JsonUtils:Start () (at Assets/Scripts/JsonUtils.cs:34)

    Any help is greatly appreciated, I'm going crazy trying to get it to work.