• 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 kPodi · May 15, 2013 at 08:16 PM · objectnullreferenceexceptionreference

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(); }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

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 kPodi · May 15, 2013 at 08:32 PM 0
Share

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

14 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 avatar image avatar image

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


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