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 /
avatar image
0
Question by DFiable · Aug 10, 2011 at 01:02 PM · errornullreferenceexceptionclonerespawn

Null Reference exception help

Would someone please help me find this Null Reference Exception?

NullReferenceException: Object reference not set to an instance of an object RespawnDieTrigger.OnTriggerEnter (UnityEngine.Collider hit) (at Assets/Troys Scripts/RespawnDieTrigger.js:17)

RespawnDieTrigger is the trigger script which is attached to the main game object(named Sphere) which is the following script. Below this script is my Respawn script which is attached to the first person controller (named FPS)

tum1-2 is the static trigger object(its a wall)

When the sphere hits the wall(tum1-2) the First Person Controller and all of its children(main camera and sphere) respawn perfectly. In my Hierarchy each time the sphere hits the wall(tum1-2) I see the FPS, FPS(Clone) FPS(Clone)(Clone)etc.. The (Clone) just adds on to the end. So what I see in the Hierarchy after five collisions is FPS(Clone)(Clone)Clone)(Clone)(Clone).Just one FirstPerson Controller with five strings of (Clone) after it. Any Ideas why I'm getting this Null Reference Exception?

 function OnTriggerEnter(hit:Collider){
         if (hit.name == "tum1-2"){
         // if player hit the wall...
         Destroy(gameObject.Find("Main Camera"));
         Destroy(gameObject);
         Destroy(gameObject.FindWithTag("GameController"));
     
              // it suicides
             // get the object tagged "Respawn"
         var objRespawn = GameObject.FindWithTag("Respawn");
            // get the script RespawnScript.js
         var script: RespawnScript = objRespawn.GetComponent(RespawnScript);
         script.RespawnPlayer(); // respawn a new player
 
          }
        }
 `

`

        var playerPrefab: GameObject; // drag the player prefab here

        function RespawnPlayer(){  
  
        Instantiate(playerPrefab, transform.position, transform.rotation);
 
 }
 
Comment
Add comment · Show 2
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
avatar image Waz · Aug 10, 2011 at 01:08 PM 2
Share

For a start, gameObject and GameObject are not the same thing. Secondly, which is line 17?

avatar image DFiable · Aug 10, 2011 at 04:03 PM 0
Share

line 17 is script.RespawnPlayer();

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Statement · Aug 10, 2011 at 04:15 PM

 function OnTriggerEnter(hit : Collider) {
     if (hit.name == "tum1-2") {    
         // This will cause null reference exception if you 
         // don't create a new "Main Camera" object somewhere
         Destroy(GameObject.Find("Main Camera")); 

         // This will cause null reference exception if you 
         //don't create a new object with "GameController" tag somewhere
         Destroy(GameObject.FindWithTag("GameController"));
 
         Destroy(gameObject);
 
         var objRespawn : GameObject = GameObject.FindWithTag("Respawn");

         // This will cause null reference exception if you 
         // have no game object with the "Respawn" tag somewhere
         var script : RespawnScript = objRespawn.GetComponent(RespawnScript);

         // This will cause null reference exception if you 
         // have no "RespawnScript" script on the object with the "Respawn" tag.
         script.RespawnPlayer();
     }
 }


Since you're getting names like "FPS(Clone)(Clone)Clone)(Clone)(Clone)", I start to think that your player prefab isn't actually a prefab, but an object in your scene. The idea is that you should create a prefab in your project and link the prefab to playerPrefab, not the actual player placed in the scene.

 var playerPrefab : GameObject;
 function RespawnPlayer() {  
     // This will cause null reference exception if you 
     // a) Forgot to set playerPrefab to a valid reference or,
     // b) Have destroyed playerPrefab, like in the case if you 
     //    linked to a scene object or,
     // c) Set it to null elsewhere in your code.
     Instantiate(playerPrefab, transform.position, transform.rotation);
 }
Comment
Add comment · Show 2 · Share
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
avatar image Statement · Aug 10, 2011 at 04:18 PM 0
Share

Note: I changed gameObject.Find etc to GameObject.Find as well. The function is static, not a member.

avatar image Waz · Aug 10, 2011 at 09:10 PM 0
Share

Exactly: so guess what object got Destroyed - this "prefab"!

avatar image
0

Answer by fredpointzero · Aug 10, 2011 at 02:10 PM

There are several places where you can have a null reference : if hit is null => "if(hit.name == "tum1-2")"

if "Respawn" is not found, objRespawn will be null and then "objRespawn.GetComponent" will throw an error

if "RespawnScript" is not found => script.RespawnPlayer will throw an error

And if I remember well, GameObject.Destroy does not accept null as argument, so if you use a "Find" method that return null, it could throw a null reference.

Hope it helps !

Comment
Add comment · Show 2 · Share
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
avatar image DFiable · Aug 10, 2011 at 04:25 PM 0
Share

thank you. I think its line 17 - script.RespawnPlayer() but I don't know why its not being found except that I've destroyed it. If this is the reason what do you suggest?

avatar image Statement · Aug 10, 2011 at 04:35 PM 0
Share

"but I don't know why its not being found except that I've destroyed it", sorry but that made me laugh. If you destroyed it, you can't find it now can you? :) It's gone. Poof. What about you destroy it after you cloned it then? But seriously, you should try making it a prefab ins$$anonymous$$d. It's getting obvious that you've linked in a scene object.

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

6 People are following this question.

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

Related Questions

Error with Update and OntrrigerEnter funtion 1 Answer

Wierd Console Errors 2 Answers

SpeechRecognitionEngine.InstalledRecognizers() returns null within Unity. 3 Answers

C# custom class array error in adding a new entry. 1 Answer

Missing Component Exception Error 3 Answers


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