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 SeaLeviathan · Jul 30, 2017 at 05:17 AM · physicsraycastarrayjointsconnectedbody

Problems with sending multiple raycasts at once

Hi, I'm trying to make a simple prototype where I have a script attached to a gameObject, and that gameObject sends a raycast out in 5 different directions, up, down, left, right, and back.

I have the general idea of how this SHOULD work, and I have gotten it to work with just a single raycast, but it seems to throw an odd missing object reference exception when i make a raycast array.

 public class Connector : MonoBehaviour
 {
     public GameObject self;
     public RaycastHit[] hit;
    void Start()
     {
         GameObject self = gameObject.GetComponent<GameObject>();
         Physics.Raycast(self.transform.position, Vector3.up, out hit[0]);   // This is line 12
         Physics.Raycast(self.transform.position, Vector3.left, out hit[1]);
         Physics.Raycast(self.transform.position, Vector3.right, out hit[2]);
         Physics.Raycast(self.transform.position, Vector3.forward, out hit[3]);
         Physics.Raycast(self.transform.position, Vector3.back, out hit[4]);
         foreach (RaycastHit connect in hit)
         {
             if(connect.transform != null)
             {
                 var joint = gameObject.AddComponent<FixedJoint>();
                 joint.connectedBody = connect.transform.GetComponent<Rigidbody>();
             }
         }
     }
 }
 

MissingComponentException: There is no 'GameObject' attached to the "Cube" game object, but a script is trying to access it. You probably need to add a GameObject to the game object "Cube". Or your script needs to check if the component is attached before using it. Connector.Start () (at Assets/Assets/Script/Connector.cs:12)

Above ^ is the error I get when i use this code, and I tried to change self.transform.position (which I know is correct syntax) with just a transform location and a vector3, with the same error. I guess it could have something with the way i raycast, but I'm still learning so I'm not sure what Raycasting limits are.

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

Answer by toddisarockstar · Jul 30, 2017 at 08:02 AM

first of all "gameobject" is NOT considered a component!!! Components attach to game objects. Also there is no need to look up a gameObject reference to itself.

secondly, when using arrays you need to initialize it by stating the length before you can do anything with it.

thirdly, with raycasting if you dont hit anything you can run into problems so it is recomended that you wrap raycasts like this into an "if" statement unless you are sure you will always get a hit.

     int i;
     public RaycastHit[] hit;
     public Vector3[] directions;
     FixedJoint joint;
     void Start(){
         hit = new RaycastHit[5];
         directions = new Vector3[]{Vector3.up, Vector3.left, Vector3.right, Vector3.forward, Vector3.back};
         i = hit.Length;while(i>0){i--;
         if(Physics.Raycast(transform.position, directions[i], out hit[i])){
                 joint = gameObject.AddComponent<FixedJoint>();
                 joint.connectedBody = hit[i].transform.gameObject.GetComponent<Rigidbody>();
             }else{print ("i didnt see anything in direction of "+directions[i].ToString());}
         }
     }
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 SeaLeviathan · Jul 30, 2017 at 03:04 PM 0
Share

Awesome, thanks! I didn't even think to make a directions array :/

Also props to you for iterating through a list with i-- lol, its usually i++ and while(i

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

135 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 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

Hover Effect with more rays as turbines? 1 Answer

Make a rolling ball always on ground without falling when reaching the edges of the map 1 Answer

Raycasting 1000 times 1 Answer

Is there a limit on the distance of raycast? 2 Answers

Physics.SphereCastAll with triggers 1 Answer


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