Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Rosh7 · Dec 06, 2017 at 03:16 AM · errorraycastraycasthit

RacastHit object not set to an instance of an object

using UnityEngine; using System.Collections;

public class Player_Seeing : MonoBehaviour {

 public GameObject player;

 Ray sight;
 Vector3 targetDir;
 RaycastHit hit;
 private float sightAngle;
 private float maxAngle = 90;

 void Start ()
 {
     
 }

 void OnTriggerStay (Collider col)
 {
     if (col.tag == "Enemy")
     {
         sight = new Ray(transform.position, col.transform.position - transform.position);
         targetDir = col.transform.position - transform.position;
         sightAngle = Vector3.Angle(targetDir, transform.forward);
         if (sightAngle <= maxAngle && Physics.Raycast(sight, out hit, 45) && hit.transform.tag == "Enemy")
         {
             hit.transform.gameObject.layer = 9;
             print("see");
         }
         else
         {
             hit.transform.gameObject.layer = 9;
             print("no see");
         }
     }
 }

}

The game I am creating is a top down game where you can only see enemies in the direction you are looking. I have the script to look and move accordingly done but now I am working on the looking aspect. It shoots a ray cast to the enemy when they are in the trigger area (a sphere around the player) and checks the angle and if there is an object in the way. if it is in these parameters than the enemy is put on a layer the camera can see, if not it is moved to a layer the camera can not see. my problem is when I run my scene in unity it pauses and says object not set to an instance of an object. I understand this means that there is not object to referance so it can't run the code. If I un-pause manually the script works as it should, moving layers when i am / am not looking at the object. Why does it give me this error?

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 Ginxx009 · Dec 06, 2017 at 03:44 AM 0
Share

Here's the problem here :

  if (sightAngle <= maxAngle && Physics.Raycast(sight, out hit, 45) && hit.transform.tag == "Enemy")

try debugging that out first for something like this:

  if (Physics.Raycast(sight, out hit, 100))
       Debug.Log(hit.collider.gameObject.name);

avatar image Rosh7 · Dec 06, 2017 at 04:30 PM 0
Share

Ok I will try that tonight. That wasn't the line that was giving me the error, it was the code in the else statement @Paul$$anonymous$$evin

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Dinosaurs · Dec 06, 2017 at 08:03 PM

The issue is this if/else:

          if (sightAngle <= maxAngle && Physics.Raycast(sight, out hit, 45) && hit.transform.tag == "Enemy")
          {
              hit.transform.gameObject.layer = 9;
              print("see");
          }
          else
          {
              hit.transform.gameObject.layer = 9;
              print("no see");
          }


if the angle check fails:

 sightAngle <= maxAngle

then the RaycastHit 'hit' is never assigned, so it's still null when you try to assign its layer. You're also assigning the same layer regardless of whether they're seen or not...

What you probably want it something more like:

                  else
                  {
                      col.gameObject.layer = 9;
                      print("no see");
                  }

which references the collider, instead of the (null) raycast hit.

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 Rosh7 · Dec 07, 2017 at 12:30 AM 0
Share

Thanks so much, i feel stupid for not realizing that. I should have read more about how ray casts work and where it gets its hit information. Cheers

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

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

Related Questions

Simple AddForce Scirpt help 1 Answer

overloaded Raycast 0 Answers

NullReference when checking tag via Raycast. How to solve this? 1 Answer

RaycastHit CS0165 Error 1 Answer

My raycast randomly stops working. 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