• 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
Question by sacajawhoohoo · Jun 30, 2016 at 04:59 AM · errorraycastraycastingnot workingraycasts

Raycast hits and affects all items within the same tag

So I am using raycast to detect player interactions in the world, and I am using tags to separate the different types of objects you can interact with. T$$anonymous$$s seemingly works fine in separating the different types of objects, but my issue is that is I have more than one object in the same tag the raycast affects all of the items in that tag instead of the single object that I want it to.

Here is the code:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class HungerAdd : MonoBehaviour {
     public GameObject self;
     public GameObject EatMeGUI;
     public GameObject Audio1;
     public Slider slider;
     public GameObject GunCheckIntObj2;
     public GameObject gameOverMan;
 
     void FixedUpdate() 
     {
         if (gameOverMan.activeSelf == false) {
             Ray mouseRay = Camera.main.ScreenPointToRay (Input.mousePosition);
             RaycastHit rayHit = new RaycastHit ();
 
             if (Physics.Raycast (mouseRay, out rayHit, 2) && (rayHit.collider.gameObject.tag == "Food") && (GunCheckIntObj2.activeSelf == false)) {
                 EatMeGUI.gameObject.SetActive (true);
             } else {
                 EatMeGUI.gameObject.SetActive (false);
             }
 
             if (Input.GetButtonDown ("Fire1") && (Physics.Raycast (mouseRay, out rayHit, 2)) && (rayHit.collider.gameObject.tag == "Food") && (GunCheckIntObj2.activeSelf == false)) {
                 EatMeGUI.gameObject.SetActive (false);
                 slider.value += 10;
                 self.gameObject.SetActive (false);
                 Audio1.gameObject.GetComponent<AudioSource> ().Play ();
             }
         }
     }
 }

The only t$$anonymous$$ng I can t$$anonymous$$nk is that the "new RaycastHit" only creates one call per script so is multiple items use the same script it affects all of those items. Am I missing somet$$anonymous$$ng here, and how would I fix it?

Comment

People who like this

0 Show 0
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
Best Answer

Answer by JigneshKoradiya · Jun 30, 2016 at 06:48 AM

 if (Physics.Raycast (mouseRay, out rayHit, 2) && (rayHit.collider.gameObject.tag == "Food") && (GunCheckIntObj2.activeSelf == false))
 
 {
    EatMeGUI.gameObject.SetActive (true);
              
 }
 else 
 {
      EatMeGUI.gameObject.SetActive (false);
              
 }


in t$$anonymous$$s if condition add rayHit.collider.gameObject.name=="yourplayrname" in OR condition so it will only work if your game object has same name

Comment
sacajawhoohoo

People who like this

1 Show 0 · 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

Answer by giorashc · Jun 30, 2016 at 07:33 AM

Yes, if your script runs on more then one object then you will have several ray casts in the scene per fixed update. Since you are using the referenced objects (instead of the object that was actually $$anonymous$$t by the ray) you see all "Food" items affected.

To solve your two problems:

  1. Use only one game object (an empty) w$$anonymous$$ch runs a script with ray collision detection for these "food" objects.

  2. When you detect a $$anonymous$$t use the rayHit.collider.gameObject to perform the logic you need on that specific object.

Comment
sacajawhoohoo

People who like this

1 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

73 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

Related Questions

Use RayCasting and simulate collision physics 1 Answer

Raycasting from object rotation? 1 Answer

Raycast returns null for no apparent reason 0 Answers

Raycast Having Pointless Errors 1 Answer

ScreenPointToRay ray not coming through from camera 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