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 zombieCraig100 · Jan 14, 2013 at 02:54 PM · raycasttransformpositionnullreferenceexceptionraycasting

NullReferenceException when using raycast

hey simply I'm trying to get an enemy to spot the player using ray casting. I'm getting the error "NullReferenceException: Object reference not set to an instance of an object" from this line of code

 var rayDirection : Vector3 = player.transform.localPosition - transform.localPosition;


Here's the main chunk of code

 var maxDistance : float;
 var player : GameObject;
 
 var rayDirection : Vector3 = player.transform.localPosition - transform.localPosition;
 
 var enemyDirection : Vector3 = transform.TransformDirection(Vector3.forward);
 var angleDot = Vector3.Dot(rayDirection, enemyDirection);
 var playerInFront = angleDot > 0.0;
 var playerClose =  rayDirection.sqrMagnitude < maxDistanceSquared;

I admit I am not a out and out programmer but I do usually work things out eventually, has me particularly confused though. Its probably something really obvious hahah

thanks for the help.

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
Best Answer

Answer by CodeMasterMike · Jan 14, 2013 at 03:01 PM

Eihter player and/or transform is not set. If you can't tell from your code which one it may be, you can do a simple check to see which one that isnt valid:

  if(player == null)
  {
    Debug.Log("player is null");
  }
  if(transform== null)
  {
    Debug.Log("transform is null");
  }

Good luck!

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 zombieCraig100 · Jan 14, 2013 at 03:07 PM 0
Share

I tried that and nothing appeared in the debug

avatar image CodeMasterMike · Jan 14, 2013 at 03:16 PM 0
Share

Well that's really odd, because the error message says one of these variables are not set.

What do you get in the debug window, if you use these lines:

    Debug.Log(player);
    Debug.Log(transform);
avatar image zombieCraig100 · Jan 14, 2013 at 03:24 PM 0
Share

thats just says

player (UnityEngine.GameObject) UnityEngine.Debug:Log(Object)

and

GameObject (UnityEngine.Transform) UnityEngine.Debug:Log(Object)

avatar image CodeMasterMike · Jan 14, 2013 at 04:07 PM 0
Share

Well, that's a really strange problem you got there. You get a NullReferenceException error even though both objects are valid...

avatar image zombieCraig100 · Jan 14, 2013 at 04:08 PM 0
Share

never$$anonymous$$d I solved the issue, well I didn't so much solve it as in I opened up Unity and it doesnt come up anymore, I don't know if was me being stupid or Unity haha thanks for your help anyway

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

10 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

Related Questions

Getting a Vector2 position depending on the rotation of an object 0 Answers

transform position and rotation of instantiated object 1 Answer

JS Dynamic Height Camera vibrating =( 1 Answer

C# Raycast goes straight into the air 2 Answers

How to make a gameobject rotation equal to ray transform? 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