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 user-12236 (google) · Apr 30, 2011 at 11:38 PM · raycastcolliderraycastingbce0019

SInce when?!?!? (Raycast issue)

okay, I don't use raycast a lot, but I've seen a pretty good deal of scripts that use a raycast and get info back from it. I copy/pasted the raycast part of this script and just changed the variable names. here's what I have

var target : Transform;

function Update () {

if (!target){

transform.Rotate(0,50,0); // raycast

var hit : RaycastHit[]; hit = Physics.RaycastAll(transform.position, transform.forward, 100.0); if (hit.collider.gameObject.tag == "PBP"){

 //target = hit.collider.gameObject;


}}}

I just want to assign the hit object as my target, but I get an error message : Assets/Scripts/BuildPoint.js(12,9): BCE0019: 'collider' is not a member of 'UnityEngine.RaycastHit[]'.

Since when is collider not a member of raycast hit? The api clearly states that it is, and I've seen loads of scripts use it. What am I doing wrong?

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

2 Replies

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

Answer by GesterX · Apr 30, 2011 at 11:55 PM

Remove the [] after RaycastHit.

By putting a [] after it you are getting an array of everything that was hit (I think - someone feel free to correct me if I'm wrong)

You are then trying to do array.collider which does not exist since hit refers to an array of hits.

EDIT: Your code is slightly off. Try this:

var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit, 100.0)) {
if (hit.collider.gameObject.tag == "PBP"){
  //do your stuff
}
}
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 user-12236 (google) · May 01, 2011 at 12:24 AM 0
Share

good thought, but now it says :

Assets/Scripts/BuildPoint.js(11,27): BCE0022: Cannot convert 'UnityEngine.RaycastHit[]' to 'UnityEngine.RaycastHit'.

avatar image user-12236 (google) · May 01, 2011 at 12:27 AM 0
Share

Also, I would think that its telling me that the raycast isnt returning an object with a collider, but then it would just stop the test of the game. ins$$anonymous$$d, it wont even compile it, so that tells me it is indeed a code error

avatar image GesterX · May 01, 2011 at 12:35 AM 0
Share

checkout my edit of my answer. It should work for you. Check out the script reference for more info http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

avatar image user-12236 (google) · May 01, 2011 at 12:37 AM 0
Share

YES! it works now! X-D Thank you very much.

avatar image GesterX · May 01, 2011 at 12:39 AM 0
Share

no worries. You can pretty much re-use this for 90% of raycast situations :-)

avatar image
0

Answer by Joshua · May 01, 2011 at 05:00 AM

The problem is not that use you RaycastAll which returns RaycastHit[] instead of Raycast which returns RaycastHit. The problem is that you don't use the information RaycastHit[] returns correctly. Instead of checking wether it is a collider with a certain tag you must check if it contains a collider with that tag. So simply loop through it using

for(i=0;i<hit.length;i++) {
    if (hit[i].collider.gameObject.tag == "PBD"){
        target = hit[i].collider.gameObject;
        return;
    }
}
Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

Raycasting ignoring small sized object 1 Answer

Raycasting Collider Problem 0 Answers

How can I let the ray to the edge of the capsule? 1 Answer

Raycast exit point of collider 0 Answers

(C#) Collider to collider2d 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