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 Vice_Versa · Jun 22, 2015 at 07:34 PM · raycastcolliderobjectsreference

Is there any way to reference the object that a raycast hits

Basically, i have a reticle that shows where the player is looking. if the player sees the object, i want to change the material color. Originally, i had this setup in the script whose material is changing, the problem was that all of the other materials colors were changing as well (i have multiple objects the player can look at that all have the same script attached). So i tried setting this up in the script for the reticle. However, it seems that i am not able to actually reference propertys of the other object through a raycast? Heres my code

 using UnityEngine;
 using System.Collections;
 
 public class reticleScript : MonoBehaviour {
     public Camera cameraFacing;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         transform.LookAt (cameraFacing.transform.position);
         transform.Rotate (0.0f, 180.0f, 0.0f);
         RaycastHit hit;
         if (Physics.Raycast (transform.position, Vector3.forward, 10, out hit)) {
             Orbit.isSelected = true;
         /*****This is where im having trobule*/

  hit.collider.gameObject.mat = Resources.Load("mat_Orange",
     typeof(Material)) as Material;
     gameObject.GetComponent<Renderer>().material = mat;
 
         }

Is what im trying to do impossible? if so, is there a better way i should be going about this?

Comment
Add comment · Show 5
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 maccabbe · Jun 22, 2015 at 07:36 PM 2
Share
 hit.collider.gameObject

http://docs.unity3d.com/ScriptReference/RaycastHit.html

avatar image Vice_Versa · Jun 22, 2015 at 07:40 PM 0
Share

its not just referencing the gameObject, i need to refence that gameObjects properties

avatar image brunopava · Jun 22, 2015 at 07:47 PM 0
Share
 $$anonymous$$aterial new$$anonymous$$aterial =  Resources.Load("mat_Orange", typeof($$anonymous$$aterial)) as $$anonymous$$aterial;
 
 // this is the material for the object that is hit by the raycast.
 // if you change this material to another it should be ok
 hit.collider.gameObject.GetComponent<Renderer>().material = new$$anonymous$$aterial;
avatar image Owen-Reynolds · Jun 22, 2015 at 11:46 PM 0
Share

Raycasting is one of the trickier things to do. $$anonymous$$eep reading the manual and examples. It sounds like some of your trouble s just because you haven't worked with Unity that long.

("i need to refence that gameObjects properties"?? If you have the gameObject, no matter how you got it, you can always mess with all the parts in it. That's a big Unity/C# trick.)

avatar image Vice_Versa · Jun 23, 2015 at 02:51 AM 0
Share

im not new to unity just never really had to use raycasting before.

1 Reply

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

Answer by AaronC · Jun 23, 2015 at 02:58 AM

Found an old post

Once you have determined that your raycast actually hit something, you can query your 'hit' object for many different properties.

For the gameObject's name:

 hit.collider.gameObject.name;

For the object's tag:

 hit.collider.tag

And there are many others - see the RaycastHit manual page.

Because "collider" is a component, you can use "GetComponent" on the hit.collider result, to get access to any other component on the gameObject - including your own script.

You would use something like this (inside your , assuming your script is called "BoomScript" and it has a function in it called "Boom":

 if (Physics.Raycast (ray, hit, 1000)) {
     var boomScript : BoomScript = hit.collider.GetComponent(BoomScript);
     if (boomScript != null) {
         boomScript.Boom();
     }
 }



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

25 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

Related Questions

Difference between hit.collider.gameObject vs hit.transform.gameObject 2 Answers

raycast not colliding 1 Answer

Checking raycast hit on tagged collider- "Cannot implicitly convert type 'string' to 'bool'" 1 Answer

RaycastHit2D hits "itself" 3 Answers

How to make the player jump down from the platform 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