Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Crimson Valor · Oct 25, 2012 at 12:48 PM · errorruntime

Unknown Error.

I have gotten an error on my project and can't figure out what the problem is, if anyone can help, i'll be thankful. Pic of error and script with problem.

 function Start () {
     Destroy(this.gameObject, 3);
     
 }
 
 function Update () {
 
 }
 
 function OnCollisionEnter(col:Collision){
     Debug.Log("collided");
     
     //Here we are searching for an object with the tag 'target01Tag'.
     var object = GameObject.FindWithTag ("target01Tag");
             
              
     
     // Here we are looking for the script called 'DestroyTarget' to use.
     var objectScript = object.GetComponent (DestroyTarget);
     
     
     //'Here we are calling the function in the script we want to use.
     objectScript.DestroyMe();
     
 }

alt text

console error.png (54.0 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Paul-Sinnett · Oct 25, 2012 at 01:13 PM

You're getting null returned from FindWithTag. So when you try to use "object" it gives you a NullReferenceException. This means "objectScript" will also be null, so you get another NullReferenceException when you try to use that.

Have you added the tag "target01Tag" to at least one object in your scene?

Comment
Add comment · Show 5 · 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 Crimson Valor · Oct 25, 2012 at 01:14 PM 0
Share

Yes, one of my Target is tagged "target01Tag"

avatar image Paul-Sinnett · Oct 25, 2012 at 01:32 PM 0
Share

Looking at your script, it looks like some kind of projectile; where the idea is that the projectile will destroy a target if it hits it. If so:

 function OnCollisionEnter(col:Collision){
     Debug.Log("collided");

     //Here we are searching for an object with the tag 'target01Tag'.
     var object = col.gameObject;
     if (object && object.CompareTag("target01Tag")) {

         Debug.Log("target01Tag object hit");

         // Here we are looking for the script called 'DestroyTarget' to use.
         var objectScript = object.GetComponent (DestroyTarget);
         if (objectScript) {

             //'Here we are calling the function in the script we want to use.
             objectScript.Destroy$$anonymous$$e();

         } else {

             Debug.Log("hit target01Tag object but DestroyTarget script not found");
         }
 
     } else {

         Debug.Log("hit something else");
    }
 }
avatar image Crimson Valor · Oct 25, 2012 at 01:38 PM 0
Share

All the "Debug.log" are co$$anonymous$$g up, but the error still remains when the bullet hits the target.

avatar image Paul-Sinnett · Oct 25, 2012 at 01:59 PM 0
Share

I've added another check to the code above to ensure that there's a DestroyTarget script attached.

avatar image Crimson Valor · Oct 25, 2012 at 02:06 PM 0
Share

No more errors appear. Thanks alot for that

avatar image
1

Answer by cfloutier · Oct 25, 2012 at 01:09 PM

easy :) the GameObject.FindWithTag ("target01Tag"); return null

or the GameObject.FindWithTag ("target01Tag"); return null

the one on line 25

Comment
Add comment · 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
0

Answer by Graham-Dunnett · Oct 25, 2012 at 01:11 PM

Hey there,

You do not have an unknown error. You have a Null Reference Exception. The console even tells you that it's on line 25 of your Destroy.js script - that's what the :25 means on the end of the line. You don't share with us what line 25 is, however, I suspect that it's the line:

 var objectScript = object.GetComponent (DestroyTarget);

but I am totally guessing. A Null Reference Exception happens when you go to use an object but it's not been set. So, I suspect the line before has failed to find a game object with the tag target01Tag, and so your variable called object is not set to anything. So, use and if() statement to check for GameObject.FindWithTag returning null.

Comment
Add comment · Show 1 · 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 Crimson Valor · Oct 25, 2012 at 01:16 PM 0
Share

Sorry, I am kinda new to Unity, What would the code look like for the if() statement.

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

12 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

Related Questions

Help with changing spririterenderer's sprite 2 Answers

Error: error CS0029: Cannot implicitly convert type `UnityEngine.GameObject[]' to `UnityEngine.GameObject 1 Answer

Do runtime errors slow down your game? 2 Answers

Main Menu Script Issues 1 Answer

BCE0043: Unexpected Token: Contact Error 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