Quantcast
Channel: Questions in topic: "error message"
Viewing all articles
Browse latest Browse all 2891

The Object you want to Instantiate is null

$
0
0
So I've been following a tutorial on making an RTS and everything was going fine till I started the Json part of the tutorial. The saving of the files went completely fine but I'm now having issues with the loading function. It loads part of the stuff fine but breaks when loading the player assets such as buildings, units etc. ---------- I've checked the code to the tutorial github and there's no difference apart from how I lay out my code so i need someone to shine some light cause I'm guessing something has changed in unity from when this tutorial was created years ago. Ive attached a screenshot of the error and the lines it refers to are unity files as well line 49 and 201 from my code listed below. ---------- ![alt text][1] [1]: /storage/temp/131964-helpscreenshot.png ---------- using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using Newtonsoft.Json; namespace RTS { public static class LoadManager { public static void LoadGame(string filename) { char seperator = Path.DirectorySeparatorChar; string path = "Saved Games" + seperator + PlayerManager.GetPlayerName() + seperator + filename + ".json"; if (!File.Exists(path)) { Debug.Log ("unable to find " + path + " Loading will crash so aborting ..."); return; } string input; using (StreamReader sr = new StreamReader (path)) { input = sr.ReadToEnd (); } if (input != null) { Debug.Log ("input is not equal to null"); using (JsonTextReader reader = new JsonTextReader (new StringReader (input))) { while (reader.Read()) { if (reader.Value != null) { if (reader.TokenType == JsonToken.PropertyName) { string property = (string)reader.Value; switch (property) { case "Sun": LoadLighting (reader); break; case "Ground": LoadTerrain (reader); break; case "Camera": LoadCamera (reader); break; case "Resources": LoadResources (reader); break; case "Players": LoadPlayers (reader); break; default : break; } } } } } } } private static void LoadLighting(JsonTextReader reader) { if (reader == null) { Debug.Log ("no reader found"); return; } Vector3 position = new Vector3 (0, 0, 0), scale = new Vector3 (1, 1, 1); Quaternion rotation = new Quaternion (0, 0, 0, 0); while (reader.Read ()) { if (reader.Value != null) { if (reader.TokenType == JsonToken.PropertyName) { if ((string)reader.Value == "Position") { position = LoadVector (reader); } else if ((string)reader.Value == "Rotation") { rotation = LoadQuaternion (reader); } else if ((string)reader.Value == "Scale") { scale = LoadVector (reader); } } } else if (reader.TokenType == JsonToken.EndObject) { GameObject sun = (GameObject)GameObject.Instantiate (ResourceManager.GetWorldObject ("Sun"), position, rotation); sun.transform.localScale = scale; Debug.Log ("this is the position of the sun" + position); return; } } } private static void LoadTerrain(JsonTextReader reader) { if (reader == null) { return; } Vector3 position = new Vector3 (0, 0, 0), scale = new Vector3 (1, 1, 1); Quaternion rotation = new Quaternion (0, 0, 0, 0); while (reader.Read ()) { if (reader.Value != null) { if (reader.TokenType == JsonToken.PropertyName) { if ((string)reader.Value == "Position") { position = LoadVector (reader); } else if ((string)reader.Value == "Rotation") { rotation = LoadQuaternion (reader); } else if ((string)reader.Value == "Scale") { scale = LoadVector (reader); } } } else if (reader.TokenType == JsonToken.EndObject) { GameObject ground = (GameObject)GameObject.Instantiate (ResourceManager.GetWorldObject ("Ground"), position, rotation); ground.transform.localScale = scale; return; } } } private static void LoadCamera(JsonTextReader reader) { if (reader == null) { return; } Vector3 position = new Vector3 (0, 0, 0), scale = new Vector3 (1, 1, 1); Quaternion rotation = new Quaternion (0, 0, 0, 0); while (reader.Read ()) { if (reader.Value != null) { if (reader.TokenType == JsonToken.PropertyName) { if ((string)reader.Value == "Position") { position = LoadVector (reader); } else if ((string)reader.Value == "Rotation") { rotation = LoadQuaternion (reader); } else if ((string)reader.Value == "Scale") { scale = LoadVector (reader); } } } else if (reader.TokenType == JsonToken.EndObject) { GameObject camera = Camera.main.gameObject; camera.transform.localPosition = position; camera.transform.localRotation = rotation; camera.transform.localScale = scale; return; } } } private static void LoadResources(JsonTextReader reader) { if (reader == null) { return; } string currValue = "", type = ""; while (reader.Read ()) { if (reader.Value != null) { if (reader.TokenType == JsonToken.PropertyName) { currValue = (string)reader.Value; } else if (currValue == "Type") { type = (string)reader.Value; Debug.Log (type); GameObject newObject = (GameObject)GameObject.Instantiate (ResourceManager.GetWorldObject (type)); Resource resource = newObject.GetComponent (); resource.LoadDetails (reader); } } else if (reader.TokenType == JsonToken.EndArray) { return; } } } private static void LoadPlayers(JsonTextReader reader) { if (reader == null) { return; } while (reader.Read ()) { if (reader.TokenType == JsonToken.StartObject) { Debug.Log("Loaded Player"); GameObject newObject = (GameObject)GameObject.Instantiate (ResourceManager.GetPlayerObject ()); Player player = newObject.GetComponent (); player.LoadDetails (reader); } else if (reader.TokenType == JsonToken.EndArray) { return; } } } public static Vector3 LoadVector(JsonTextReader reader) { Vector3 position = new Vector3 (0, 0, 0); if (reader == null) { return position; } string currValue = ""; while (reader.Read ()) { if (reader.Value != null) { if (reader.TokenType == JsonToken.PropertyName) { currValue = (string)reader.Value; } else { switch (currValue) { case"x": position.x = (float)LoadManager.ConvertToFloat(reader.Value);; break; case"y": position.y = (float)LoadManager.ConvertToFloat(reader.Value);; break; case"z": position.z = (float)LoadManager.ConvertToFloat(reader.Value);; break; default: break; } } } else if(reader.TokenType == JsonToken.EndObject) { return position; } } return position; } public static Quaternion LoadQuaternion(JsonTextReader reader) { Quaternion rotation = new Quaternion (0, 0, 0, 0); if (reader == null) { return rotation; } string currValue = ""; while (reader.Read ()) { if (reader.Value != null) { if (reader.TokenType == JsonToken.PropertyName) { currValue = (string)reader.Value; } else { switch (currValue) { case"x": rotation.x = (float)LoadManager.ConvertToFloat(reader.Value);; break; case"y": rotation.y = (float)LoadManager.ConvertToFloat(reader.Value);; break; case"z": rotation.z = (float)LoadManager.ConvertToFloat(reader.Value);; break; case"w": rotation.w = (float)LoadManager.ConvertToFloat(reader.Value);; break; default: break; } } } else if(reader.TokenType == JsonToken.EndObject) { return rotation; } } return rotation; } public static Color LoadColour(JsonTextReader reader) { Color colour = new Color (0, 0, 0, 0); string currValue = ""; while (reader.Read ()) { if (reader.Value != null) { if (reader.TokenType == JsonToken.PropertyName) { currValue = (string)reader.Value; } else { switch (currValue) { case"r": colour.r = (float)LoadManager.ConvertToFloat(reader.Value);; break; case"g": colour.g = (float)LoadManager.ConvertToFloat(reader.Value);; break; case"b": colour.b = (float)LoadManager.ConvertToFloat(reader.Value);; break; case"a": colour.a = (float)LoadManager.ConvertToFloat(reader.Value);; break; default: break; } } } else if(reader.TokenType == JsonToken.EndObject) { return colour; } } return colour; } public static List LoadStringArray(JsonTextReader reader) { List values = new List (); while (reader.Read ()) { if (reader.Value != null) { values.Add ((string)reader.Value); } else if (reader.TokenType == JsonToken.EndArray) { return values; } } return values; } public static Rect LoadRect(JsonTextReader reader) { Rect rect = new Rect (0, 0, 0, 0); if (reader == null) { return rect; } string currValue = ""; while (reader.Read ()) { if (reader.Value != null) { if (reader.TokenType == JsonToken.PropertyName) { currValue = (string)reader.Value; } else { switch (currValue) { case "x": rect.x = (float)LoadManager.ConvertToFloat(reader.Value);; break; case "y": rect.y = (float)LoadManager.ConvertToFloat(reader.Value);; break; case "width": rect.width = (float)LoadManager.ConvertToFloat(reader.Value);; break; case "height": rect.height = (float)LoadManager.ConvertToFloat(reader.Value); break; default: break; } } } if (reader.TokenType == JsonToken.EndObject) { return rect; } } return rect; } public static float ConvertToFloat(object value) { float val1 = new float(); if(value.GetType() == typeof(long)) val1 = (float)(long) value; if(value.GetType() == typeof(double)) val1 = (float)(double) value; return val1; } } } ---------- For clarity as the code is rather long :L line 201 is in the LoadResources function and starts with the GameObject newObject = (GameObject)Instantiate(ResourceManager.GetWorldObject(type)). The problem I have is that this exact line is used fine for loading the terrain with no problems. So I'm confused :L. Thanks for reading and any help will be massively appreciated. A link to the tutorial -> http://stormtek.geek.nz/rts_tutorial/part19.php

Viewing all articles
Browse latest Browse all 2891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>