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 Rxanadu · Jun 02, 2013 at 02:42 AM · raycastprefabscomponenttags

Activating only one of many prefabs with the same tag

My game will be using signs to use tell the player different objectives they will have to accomplish it. These signs will hold hidden messages which only appear when the player is facing directly at them. In order to accomplish this, the player character will project a ray onto the sign's collider. When it does hit the sign, the message appears. I have that part of the sign's functionality set up; my trouble comes with making sure the sign the player is looking at is the only sign that shows its hidden message.

If my ray hits one of the colliders on the sign, all of the signs activate, not just the one the ray hits. I'm not quite sure what to do at this point. I've looked all over for help with this issue, but none of them help with keeping one out of a number of prefabs active. Here is the class that activates the signs when the player looks at a sign:

 public class SignContentFinder : MonoBehaviour {
 
     GameObject[] interactSigns;
 
     void Awake() {
         interactSigns = GameObject.FindGameObjectsWithTag("SignContent");
     }
     
     // Update is called once per frame
     void Update()
     {
         Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
         RaycastHit hit;
         foreach (GameObject sign in interactSigns)
         {
             if (Physics.Raycast(ray, out hit, 5f))
             {
                 sign.renderer.material.mainTexture = sign.GetComponent<SignContentController>().secretTex;
             }
             else
                 sign.renderer.material.mainTexture = sign.GetComponent<SignContentController>().mainTex;
         }
     }
 }

Any help would be appreciated. I would like to actually get some feedback this time, as opposed to the 5-6 recent questions I've posted with no feedback.

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 PAEvenson · Jun 02, 2013 at 03:05 AM

It is because of your forloop. You should be checking the hit variable to see if that is actually the sign youre hitting. I would do something like this(not tested >_<):

  void Update()
     {
         foreach (GameObject sign in interactSigns)
         {
              //set all our signs to not show our secret text
              sign.renderer.material.mainTexture = sign.GetComponent<SignContentController>().mainTex;
  
         }
         Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit, 5f))
             {
                   SignContentController controller = hit.collider.gameObject.GetComponent<SignContentController>();
                   if(controller != null)//if we are null it isnt a sign >_<
                   {
                        controller.gameObject.renderer.material.mainTexture = controller.secretTex;
                   }
             }
     }
Comment
Add comment · Show 6 · 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 Owen-Reynolds · Jun 02, 2013 at 05:02 AM 0
Share

The main idea is that ray casts don't need a loop since they have a secret loop in them -- "shooting" the ray just once checks every sign (but stops when it hits one.)

Then the other is the raycast tells you what if hit, using the output HIT variable. You don't have to shoot it "at" anything specific, and it will still say what it hits.

avatar image Rxanadu · Jun 02, 2013 at 11:21 AM 0
Share

I was able to incorporate the code into my script, and it worked. I still needed to add my check to turn all of the signs back into the main texture, as I want the message to disappear when the player isn't looking at it.

Either way, thanks PAEvenson for the help.

As for Owen: I'm still not sure I follow. Are you saying if I removed my foreach loop, would the code work the same?

avatar image Owen-Reynolds · Jun 02, 2013 at 03:55 PM 0
Share

Your original "what's in front of me" raycast was in a loop. No need for that (notice how PAvanson's code just raycasts once.)

avatar image Rxanadu · Jun 02, 2013 at 04:38 PM 0
Share

I just noticed that. Thanks for pointing that out.

So the foreach loop now just sets all of the signs not hit by the ray to its initial message.

$$anonymous$$eanwhile, the Physics.Raycast check has a reference solely for the collider that was hit by the ray. This reference will then change that sign's collider to the secret message only when the collider is hit by the ray.

Have I got that right?

avatar image Owen-Reynolds · Jun 02, 2013 at 09:27 PM 0
Share

I think, yes. The raycast "I hit this" is the var hit. Then you can see, next line, where the code uses hit to look up the sign (or next line, where it says "hey, HIT wasn't a sign!")

The loop just happens to be how you do sign record-keeping. Nothing to do with the raycast.

Show more comments

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

16 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

Related Questions

How to fill selected area with prefabs? 1 Answer

Logic problem with RaycastHit and prefabs scripts 0 Answers

[Problem] Cant activate 2D objects with touch? 1 Answer

if hit.collide.tag is not working on raycast function 1 Answer

Raycast that ignores object with certain tags? 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