- Home /
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; }
This is a silly question, but did you assign the teleporter Transform in the inspector?
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.
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)
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.
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

Follow this Question
Related Questions
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