Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ReggieBeRetro · Jul 11, 2016 at 04:51 AM · raycasthitnull reference exceptionobject referencewhile-loopobject-reference-error

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;
 
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

59 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges