Errors appear when i get a bad Raycast hit. Meaning when it hits another object in the scene.
The error happens on line 57 at random with no rhyme or reason.
I have multiple empty game objects with this script attached to each object separately.
The script works and shows when a good or bad hit is received So i dont know why the error is showing if the debug is getting through. Im getting between 3 to 6 errors of the same type.
NullReferenceException: Object reference not set to an instance of an object RandomizeObj.Start () (at Assets/Scripts/RandomizeObj.cs:57)
All objects are tagged correctly and and the RayCast is a successful hit.
I should mention thta it was working fine untill i added these four conditions to the while loop.
(hit.collider.gameObject.tag == "Roads") || (hit.collider.gameObject.tag == "Debris") || (hit.collider.gameObject.tag == "Pickup") || (hit.collider.gameObject.tag == "Buildings")
Once again spelling is correct and the tags match. Also when i look in hierarchy and select a generated object i can see that the tag is assigned.
The while loop is to ensure that a new object does not fall on something that is placed in the scene already.
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 
 public class RandomizeObj : MonoBehaviour {
     public List<Transform> ObjList = new List<Transform>();
     //public Transform obj;
     public Vector3 postiion;
     public GameObject objspawned;
     public int totalobjs;
     private int ObjNum;
     public float ObjectsetWidth;
     public float ObjectsetLength;
     public float respawntime = 3600.0f;
     public float respawn = 3600.0f;
     public Text ObjText;
     public Text ObjText2;
     public Transform newObj;
     //respawn object if destryed after 60minutes - 3600 Seconds - 3600000.0f milliseconds
 
 
     void Start () {
 
         int spawned = 0;
 
         while (spawned < totalobjs) {
             Vector3 position = new Vector3 (transform.position.x + Random.value * ObjectsetWidth, transform.position.y, transform.position.z + Random.value * ObjectsetLength);
 
             RaycastHit hit = new RaycastHit ();
 
             if (Physics.Raycast (position, Vector3.down, out hit)) {
                 ObjNum = Random.Range (0,ObjList.Count);
                 if ((hit.collider.gameObject.tag != "Tree") && (hit.collider.gameObject.tag != "Stone") && (hit.collider.gameObject.tag != "Bush") && (hit.collider.gameObject.tag != "Roads") && (hit.collider.gameObject.tag != "Debris") && (hit.collider.gameObject.tag != "Pickup") && (hit.collider.gameObject.tag != "Buildings")) {
                     Debug.Log ("Good hit");
                     Transform newObj = (Transform)Instantiate (ObjList[ObjNum], hit.point, Quaternion.identity);
                     newObj.transform.SetParent (transform);
 
                     newObj.name = gameObject.name + "_Obj" + spawned;
                     ObjText = GameObject.Find ("ObjectText").GetComponent<Text> ();
                     ObjText2 = GameObject.Find ("ObjectText2").GetComponent<Text> ();
 
                     ObjText.name = gameObject.name  + "_Obj" + spawned  + "_ObjectText";
                     ObjText2.name = gameObject.name  + "_Obj" + spawned + "_ObjectText2";
 
                     spawned++;
 
                 } else {
                     
 
                     position = new Vector3 (transform.position.x + Random.value * ObjectsetWidth, transform.position.y, transform.position.z + Random.value * ObjectsetLength);
 
                     hit = new RaycastHit ();
 
                     if (Physics.Raycast (position, Vector3.down, out hit)) {
                         if (gameObject) {
                             while ((hit.collider.gameObject.tag == "Tree") || (hit.collider.gameObject.tag == "Stone") || (hit.collider.gameObject.tag == "Bush") || (hit.collider.gameObject.tag == "Roads") || (hit.collider.gameObject.tag == "Debris") || (hit.collider.gameObject.tag == "Pickup") || (hit.collider.gameObject.tag == "Buildings")) {
                                 
                                 Debug.Log ("Bad Hit - " + gameObject.name + " at " + hit.collider.gameObject.tag);
 
                                 position = new Vector3 (transform.position.x + Random.value * ObjectsetWidth, transform.position.y, transform.position.z + Random.value * ObjectsetLength);
 
                                 hit = new RaycastHit ();
 
 
                             }
                         }
                     }
 
                     if (Physics.Raycast (position, Vector3.down, out hit)) {
                         Transform newObj = (Transform)Instantiate (ObjList[ObjNum], hit.point, Quaternion.identity);
                         newObj.transform.SetParent (transform);
 
                         newObj.name = gameObject.name + "_Obj" + spawned;
 
                         ObjText = GameObject.Find ("ObjectText").GetComponent<Text> ();
                         ObjText2 = GameObject.Find ("ObjectText2").GetComponent<Text> ();
                     
                         ObjText.name = gameObject.name  + "_Obj" + spawned + "_ObjectText";
                         ObjText2.name = gameObject.name  + "_Obj" + spawned + "_ObjectText2";
 
 
                         spawned++;
                     }
                 
 
                 }
 
             }
 
         }
 
 
 
     }
     void Update() {
         respawntime -= Time.deltaTime;
         if (respawntime < 0) {
             CheckForObjs ();
         }
     }
     void CheckForObjs() {
         int i = 0;
         while (i < totalobjs) {
 
 
 
             if (objspawned == null) {
                 Vector3 position = new Vector3 (transform.position.x + Random.value * ObjectsetWidth, transform.position.y, transform.position.z + Random.value * ObjectsetLength);
                 ObjNum = Random.Range (1,ObjList.Count);
                 objspawned = GameObject.Find(gameObject.name + "_Obj" + i);
                 RaycastHit hit = new RaycastHit ();
 
 
                 while ((hit.collider.gameObject.tag == "Tree") || (hit.collider.gameObject.tag == "Stone") || (hit.collider.gameObject.tag == "Bush") || (hit.collider.gameObject.tag == "Roads") || (hit.collider.gameObject.tag == "Debris") || (hit.collider.gameObject.tag == "Pickup") || (hit.collider.gameObject.tag == "Buildings")) {
                     position = new Vector3 (transform.position.x + Random.value * ObjectsetWidth, transform.position.y, transform.position.z + Random.value * ObjectsetLength);
 
                     hit = new RaycastHit ();
                 }
                 if (Physics.Raycast (position, Vector3.down, out hit)) {
                     
                     Transform newObj = (Transform)Instantiate (ObjList[ObjNum], hit.point, Quaternion.identity);
                     newObj.transform.SetParent (transform);
 
                     newObj.name = gameObject.name + "_Obj" + i;
 
                     Debug.Log ("Not Found Object at " + i);
                 } 
             }
 
             i++;
         }
         respawntime = respawn;
 
     }
 }
 
Your answer
 
 
             Follow this Question
Related Questions
Null Reference Exception anytime scene is reloaded? 1 Answer
Error That Doesn't Affect My Game Is Spammed Every Frame. 1 Answer
Unknown Cause of NullReferenceException? 1 Answer
Error Object reference not set to an instance of an object!!! Help, I am new! 0 Answers
object reference required to to access non-static member, userSession 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                