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 THaynes · Jul 15, 2014 at 04:05 AM · javascriptnullreferenceexception

Null Reference issue Teleporting player in js

I have a jscript that teleports objects tagged "Player" to a certain destination when it(they) enter the object with the attached script's box collider. The script has been working fine for a while, then suddenly I got this 'Object reference not set to an instance of an object' error, even though I made no changes to the script. Can anyone help with this?

Here is the script:

var teleport : Transform;

function OnTriggerEnter (col : Collider) { var player = gameObject.Find("Player");

 if (col == player.collider) {
 player.transform.position = teleport.position;

// Application.LoadLevel("youwin"); } else { Destroy(col.gameObject); } }

function Reset () { if (collider == null)
gameObject.AddComponent(BoxCollider); collider.isTrigger = true; }

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 Anti-Social Fred · Jul 15, 2014 at 05:19 AM 0
Share

This is a silly question, but did you assign the teleporter Transform in the inspector?

avatar image THaynes · Jul 15, 2014 at 07:40 AM 0
Share

The teleporter Transform is assigned by putting in an empty gameobject (in some set location) in the teleport component public drop box.

Here is the error:

NullReferenceException: Object reference not set to an instance of an object teleportall.OnTriggerEnter (UnityEngine.Collider col) (at Assets/Scripts/teleportall.js:6)

I guess I really don't know how to deal with null errors, because I setup another scene real quick and tested the script and it works fine.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by THaynes · Jul 15, 2014 at 07:55 AM

when attaching the teleport script as a component of (how I'm using it) an empty game object it shows a public drop box where I drop in another empty game object that will serve as the destination of the teleporter Transform.

Here is the error:

NullReferenceException: Object reference not set to an instance of an object teleportall.OnTriggerEnter (UnityEngine.Collider col) (at Assets/Scripts/teleportall.js:6)

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 Anti-Social Fred · Jul 15, 2014 at 02:45 PM

Only other thing I can think of is that your player is null. To be extra-safe (and completely overkill) use this:

         function OnTriggerEnter(Collider c)
         {
             if (c == null)
             {
                 Debug.LogError("Problem! Collider is null! This SHOULDN'T happen!");
                 return;
             }
             if (player == null)
             {
                 Debug.LogError("Player is null.");
                 return;
             }
 
             // The rest of your code
         }

put the rest of your code under the "// The rest of your code"

I wrote it in C# but it's very close... just change with the proper unityscript syntax. This will debug the player object, and debug the collider object. You also should Debug the teleporter object.

If you haven't found the error after this i'll help further but this SHOULD cover it.

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 THaynes · Jul 19, 2014 at 11:50 PM 0
Share

Thank you guys for the responses. It turns out I had the variable finding the object to be teleported looking for an object (the player) with the wrong name, so it couldn't reference the object. Derp. Simple fix.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

"NullReferenceException" Error when trying to count objects with a particular tag using GameObject.FindGameObjectsWithTag function 1 Answer

Another Null Reference Exception 1 Answer

Help with eval() 0 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

NullReferenceException 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