- Home /
NullReferenceException: Object reference not set to an instance of an object
Hello everyone,
I just purchased Top-Down Dungeons [Mobile] and added all my game prefabs correctly. But when I try to walk (move) with my player, I get this fatal error:
NullReferenceException: Object reference not set to an instance of an object PlayerScript.CheckTerrain () (at Assets/Scripts/PlayerScript.js:162) PlayerScript.MovePlayer () (at Assets/Scripts/PlayerScript.js:200) PlayerScript.FixedUpdate () (at Assets/Scripts/PlayerScript.js:41)
Can anyone help me to fix this?
159-164 lines:
function CheckTerrain() {
var MoveTarget = GameObject.Find("MoveTarget").transform.position;
var MyY = transform.position;
if (Terrain.activeTerrain.SampleHeight(MoveTarget) -1 > Terrain.activeTerrain.SampleHeight(MyY)) return false;
else return true;
}
191-204 lines:
if (Input.GetKey ("mouse 1")) {
if (Input.mousePosition.x >= Screen.width - Screen.width/6) { if (!animation.IsPlaying("idle")) AnimationPlay("idle"); return; }
playerPlane = new Plane(Vector3.up, transform.position);
ray = GameObject.Find("Main Camera").camera.ScreenPointToRay (Input.mousePosition);
hitdist = 0.0;
if (playerPlane.Raycast (ray, hitdist)) {
targetPos = Vector3(ray.GetPoint(hitdist).x,0,ray.GetPoint(hitdist).z);
transform.rotation = Quaternion.LookRotation(targetPos - Vector3(transform.position.x,0,transform.position.z));
if (Vector3.Distance(targetPos,transform.position)<9) { animName = "walk"; speed = 5; } else { animName = "run"; speed = 12; }
if (MoveCanSee() == true && CheckTerrain() == true) transform.Translate(Vector3.forward * Time.deltaTime * speed);
if (IsMultiplayer == 1) UpdateTransform();
}
if (!animation.IsPlaying(animName) && !animation.IsPlaying("spellcast")) AnimationPlay(animName);
}
and 39-42 lines: function FixedUpdate () { if (IsMultiplayer == 1 && nextCheckMP < Time.time) { nextCheckMP = Time.time + 1; CheckMP(); } if (Died == 0) MovePlayer(); }
Answer by Graham-Dunnett · May 15, 2013 at 08:18 PM
Line 162 of your PlayerScript.js uses a variable called Terrain
. This variable hasn't been initialised.
Thank you for your answer Sir, I really appreciate it. But I'm not a really good programmer, so, can you help me how to fix it? It would help me alot. Thanks in advance.
Your answer
Follow this Question
Related Questions
NullReferenceException: Object reference not set to an instance of an object ? 1 Answer
NullReferenceException: Object reference not set to an instance of an object - MonoBehaviour 2 Answers
The infamous: Object Reference not set to an instance of an object 1 Answer
" NullReferenceException: Object reference not set to an instance of an object" 3 Answers
NullReferenceException: Object reference not set to an instance of an object 0 Answers