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 vereonis · Nov 27, 2019 at 08:14 PM · raycasttransformraycastingtransform.position

Unable to apply "transform" on gameobject from Raycast

I have 2 blocks, Block A is the player, Block B is part of a cube grid floor. I want to get the position of Block B from Block A. There are many Block Bs in the Scene.

I'm able to destroy the specific Block B the player is above (I'm using forward on the raycast this is correct), but can't get it's position. S o Ican zip around deleting the floor as I go, but can't get the position of any of these game objects :/

 private float rayBlockCheckDistance = 10;
 private void gridSnap()
 {
     RaycastHit[] hits;
     hits = Physics.RaycastAll(transform.position, Vector3.forward, rayBlockCheckDistance);     

     GameObject gFloor = hits[0].transform.gameObject;
     //Transform tFloor = gFloor.transform;  //This Errors
     //Vector3 position = new Vector3(tFloor.position); //So this Errors aswell

     Destroy(gFloor); //But I can destroy the specific Block fine.
 }

No errors are given for why I can't apply transform as thus get the position of the first object in the raycast array, the Editor just flashes "Compiler errors", but no errors are given in console or VS.

Both objects have box colliders/rigid bodies.

Comment
Add comment · Show 1
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 Happeloy · Nov 28, 2019 at 07:05 AM 0
Share

Other than what Larry-Dietz said, it also looks a bit concerning that you get hits[0] before checking that there actually are any hits. You should add a check if hits has a length greater than 0.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Larry-Dietz · Nov 27, 2019 at 11:03 PM

Looks like you are trying to set tFloor equal to an undeclared DirectFloor variable, Unless this is being initialized somewhere else. From looking at your code, I would first try changing that line to

Transform tFloor = gFloor.transform;

Then on the line below, change it to...

Vector3 position = tFloor.position;

The position variable of the transform is already a Vector3

One last thing, in your Destroy line, you can use Destroy(gFloor); since gFloor is already set to hits[0].transform.gameObject.

Hope this helps, -Larry

Comment
Add comment · Show 2 · 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 vereonis · Nov 28, 2019 at 09:08 AM 0
Share

thanks for the response, "DirectFloor" was a typo by me, I've updated my code and the question, however I'm still getting "compiler error" on 'Transform tFloor = gFloor.transform'. Destroy works using gFloor so it clearly does contain the specific game object, but unable to access its transform.

avatar image Larry-Dietz vereonis · Nov 28, 2019 at 08:43 PM 0
Share

I'll admit, you have me stumped. All game objects contain a transform as far as I know. And if your gFloor object is being removed from the scene when the destroy call runs, then you are right, gFloor must be populated.

When it errors, exactly what is the error message saying?

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

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

How do i get the transform of an object with raycasthit? 1 Answer

Linecast blocked by colliders not actually in the way? 0 Answers

C# Raycast goes straight into the air 2 Answers

NullReferenceException when using raycast 1 Answer

Maintain object position when using raycasting to multiple height 2 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